【问题标题】:PHP - For each loop problemsPHP - 对于每个循环问题
【发布时间】:2012-01-14 22:51:19
【问题描述】:

在 Wordpress 中,我尝试从头开始创建元框脚本,以更好地理解 Wordpress 和 PHP。

我在多维数组上的 for each 循环中遇到了一些问题。我正在使用 PHP5。

这是数组:

$meta_box = array();    
$meta_box[] = array(
            'id' => 'monitor-specs',
            'title' => 'Monitor Specifications',
            'context' => 'normal',
            'priority' => 'default',
            'pages' => array('monitors', 'products'),
            'fields' => array(
                array(
                    'name' => 'Brand',
                    'desc' => 'Enter the brand of the monitor.',
                    'id' => $prefix . 'monitor_brand',
                    'type' => 'text',
                    'std' => ''
                )
            )
        );

这是 for each 循环:

foreach ($meta_box['pages'] as $post_type => $value) {
            add_meta_box($value['id'], $value['title'], 'je_format_metabox', $post_type, $value['context'], $value['priority']);
        }

我要做的是循环遍历“pages”数组中的键,该数组是“meta_box”数组中的一个数组,同时能够使用“meta_box”数组的键值。

我需要为每个循环嵌套一些吗?

非常感谢一些正确方向的指点,以便我可以解决这个问题。

【问题讨论】:

  • 您想对页面中的这些项目做什么?
  • 如果 $meta_box 已经描述了数组,你会得到 $post_type 0,1 和空的 $value['context'], $value['priority']。但你的方式应该是空数组,因为 $meta_box['pages'] 是空的
  • to better understand both Wordpress and PHP. [rant]Wordpress 并不是最好的学习代码[/rant]

标签: php arrays wordpress foreach meta-boxes


【解决方案1】:

您的foreach$meta_box['pages'] 开头,但没有$meta_box['pages']

你确实有一个$meta_box[0]['pages'],所以你需要两个循环:

foreach($meta_box as $i => $box)
    foreach($box['pages'] as $page)
        add_meta_box(.., ..); // do whatever

您希望在您的 $value 变量中包含什么?

【讨论】:

    【解决方案2】:
    foreach ($meta_box[0]['pages'] as $post_type => $value) {
    

    $meta_box = array(...
    

    【讨论】:

    • 这个答案以及来自@ajreal 的答案帮助我解决了这个问题!我最终使用了$meta_box = array(...
    【解决方案3】:

    这里是:

    $meta_box = array();    
    $meta_box[] = array(......
    

    建议没有 $meta_box['pages']。 meta_box 是一个带有数字索引的数组(检查 [] 运算符),它的每个元素都是一个包含键“pages”的数组。

    所以你需要在 $meta_box 上使用 foreach,并且在每个元素上你需要使用 pages 键。id、title、context 是与页面处于同一级别的元素,如你所见

    【讨论】:

      【解决方案4】:

      你引用了错误的数组键

      $meta_box[] <-- $meta_box[0]
      

      但是,你指的是使用:-

      foreach ($meta_box['pages'] as $post_type => $value) {
      

      添加数组键即可解决问题:-

      foreach ($meta_box[0]['pages'] as $post_type => $value) {
      

      【讨论】:

        【解决方案5】:

        也许最好创建一些类来保存这些信息。

        class Metabox
        {
          public $id, $title, $context, $priority, $pages, $fields;
        
          public function __construct($id, $title, $pages, $fiels, $context='normal', $priority='default')
          {
            $this->id = $id;
            $this->title = $title;
            $this->pages = $pages;
            $this->fields = $fields;
            $this->context = $context;
            $this->priority = $priority;
          }
        
        }
        
        $meta_box = array();
        
        $meta_box[] = new Metabox(
          'monitor-specs', 
          'Monitor Specifications', 
          array('monitors', 'products'),
          array(
            'name' => 'Brand',
            'desc' => 'Enter the brand of the monitor.',
            'id' => $prefix . 'monitor_brand',
            'type' => 'text',
            'std' => ''
          )
        );
        

        现在你可以像这样遍历 meta_box 数组:

        foreach ($meta_box as $box)
        {
          add_meta_box($box->id, $box->title, .. and more)
          // This function could be placed in the metabox object
        
          /* Say you want to access the pages array : */
          $pages = $box->pages;
        
          foreach ($pages as $page)
          {
            ..
          }
        }
        

        现在你仍然有一个循环中的循环,但可能有助于更清楚地看到你的问题。

        【讨论】:

        • 我需要学习如何创建类!这是我似乎无法让我的大脑理解的事情之一......希望有一天,它会让代码变得更加简单和漂亮!
        • 在我住的地方,我们称之为“用显微镜敲钉子”:) +1 无论如何都是好的解决方案
        猜你喜欢
        • 2011-07-15
        • 2017-12-16
        • 2023-04-09
        • 1970-01-01
        • 2017-12-10
        • 1970-01-01
        • 2013-11-22
        • 2020-05-11
        相关资源
        最近更新 更多