PHP - 对于每个循环问题
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或者
or
您的
foreach
以$meta_box['pages']
开头,但没有$meta_box['pages']
。不过,您确实有一个
$meta_box[0]['pages']
,因此您需要两个循环:您希望
$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:What were you expecting to be in your
$value
variable?这里:
表明没有 $meta_box['pages']。 meta_box 是一个带有数字索引的数组(检查 [] 运算符),它的每个元素都是一个具有键“pages”的数组。
所以你需要在$meta_box上使用foreach,并且在每个元素上你需要使用pages key.. id,title,context是与pages处于同一级别的元素,如你所见
this here:
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
您引用了错误的数组键
但是,您引用使用:-
添加数组键将解决问题:-
You are referencing to the wrong array key
But, you refer using :-
Add the array key will solve the problem :-
也许创建一些类来保存这些信息可能会很好。
现在您可以像这样循环遍历 meta_box 数组:
现在您仍然有一个循环中的循环,但也许有助于更清楚地看到您的问题。
Maybe it could be nice to create some class to hold this information.
Now you can loop over the meta_box array like:
Now you still have a loop in a loop, but maybe helps seeing your problem more clearly.