我有以下数组
$meta_boxes[] = array(
'id' => 'measurements',
'title' => 'Measurements',
'fields' => array(
array(
'name' => 'Select Units of Measurement',
'id' => 'units',
'type' => 'radio',
'options' => array(
array('name' => 'Pounds', 'value' => 'Pounds'),
array('name' => 'Kilos', 'value' => 'Kilos'),
array('name' => 'Ton', 'value' => 'Ton'),
array('name' => 'Short Ton', 'value' => 'Short Ton')
)
),
array(
'name' => 'Displacement',
'id' => 'displacement',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Gross Tonnage',
'id' => 'gross_tonnage',
'type' => 'text',
'std' => ''
)
)
)
//more meta_boxes[] arrays continued...
当使用foreach循环从fields
数组中的数组中获取元素时,如何省略一个数组?例如,省略循环遍历id为units
的'fields'中的第一个数组?或者任何其他阵列。
foreach ($meta_boxes as $metabox) {
foreach ( $metabox['fields'] as $field ) {
echo $field['name']; //field name
}
}
答案 0 :(得分:2)
foreach ($meta_boxes as $metabox) {
foreach ( $metabox['fields'] as $field ) {
if ($field['id'] == 'units') continue;
echo $field['name']; //field name
}
}
答案 1 :(得分:2)
使用关键字continue
和if条件继续循环中的下一个实体。
要使用特定$field
跳过多个id
,我建议您使用
foreach ($meta_boxes as $metabox) {
foreach ( $metabox['fields'] as $field ) {
if (in_array ($field['id'], array ('units', 'gross_tonnage'))
continue;
echo $field['name']; //field name
}
}
如果只有一个人使用这样的东西:
foreach ($meta_boxes as $metabox) {
foreach ( $metabox['fields'] as $field ) {
if ($field['id'] == 'units')
continue;
echo $field['name']; //field name
}
}