PHP - 对于每个循环问题

发布于 2024-12-20 00:17:06 字数 1151 浏览 6 评论 0原文

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

不过,我在多维数组上的 for every 循环方面遇到了一些问题。我正在使用 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' => ''
                )
            )
        );

这是 foreach 循环:

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' 数组的键值。

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

将不胜感激一些正确方向的指示,以便我可以解决这个问题。

In Wordpress I'm trying to create a metabox script from scratch to better understand both Wordpress and PHP.

I'm having some problems with a for each loop on a multidimensional array though. I'm using PHP5.

This is the array:

$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' => ''
                )
            )
        );

And this is the for each loop:

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

What I'm trying to do is loop through the keys in the 'pages' array which is an array inside the 'meta_box' array and at the same time be able to use the key values of the 'meta_box' array.

Do I need to nest some for each loops?

Would be grateful for some pointers in the right direction so I can solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜中书 2024-12-27 00:17:06
foreach ($meta_box[0]['pages'] as $post_type => $value) {

或者

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

or

$meta_box = array(...
懷念過去 2024-12-27 00:17:06

您的 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 变量中有什么?

Your foreach starts with $meta_box['pages'], but there is no $meta_box['pages'].

You do have a $meta_box[0]['pages'] though, so you need two loops:

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

What were you expecting to be in your $value variable?

烟花易冷人易散 2024-12-27 00:17:06

这里:

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

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

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

this here:

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

suggests that there is no $meta_box['pages']. meta_box is an array with numerical indexes (check the [] operator) and each of its elements is an array that has the key 'pages'.

so you need to use foreach on $meta_box, and on each element you need to use the pages key.. id, title, context are elements on the same level as pages, as you can see

一杯敬自由 2024-12-27 00:17:06

您引用了错误的数组键

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

但是,您引用使用:-

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

添加数组键将解决问题:-

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

You are referencing to the wrong array key

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

But, you refer using :-

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

Add the array key will solve the problem :-

foreach ($meta_box[0]['pages'] as $post_type => $value) {
静待花开 2024-12-27 00:17:06

也许创建一些类来保存这些信息可能会很好。

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)
  {
    ..
  }
}

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

Maybe it could be nice to create some class to hold this information.

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' => ''
  )
);

Now you can loop over the meta_box array like:

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)
  {
    ..
  }
}

Now you still have a loop in a loop, but maybe helps seeing your problem more clearly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文