多维数组-如何从子数组中获取特定值 [英] Multidimensional array - how to get specific values from sub-array
本文介绍了多维数组-如何从子数组中获取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下数组结构:
Array
(
[0] => Array
(
[product_option_id] => 236
[option_id] => 14
[name] => Masura S
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 33
[option_value_id] => 53
[name] => Alb
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 35
[option_value_id] => 55
[name] => Rosu
[price] =>
[price_prefix] => +
)
)
[required] => 0
)
[1] => Array
(
[product_option_id] => 237
[option_id] => 15
[name] => Masura M
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 34
[option_value_id] => 58
[name] => Rosu
[price] =>
[price_prefix] => +
)
)
[required] => 0
)
)
我发现自己迷失了试图显示此数组中所有[name]值的机会.
I find myself lost in trying to display all the [name] values from this array.
我想做的是根据第一级[name]
(如[name] => Masura S
)填充带有下拉选择的表单,然后根据第二级[name]
(如[name] => Alb
)填充第二个下拉选择
What I am trying to do is to populate a form with dropdown selects based on the first level [name]
(like [name] => Masura S
) and then a second dropdown select with the second level [name]
(like [name] => Alb
).
如果您有任何指点,我将不胜感激...
I would appreciate it if you have any pointers...
推荐答案
您可以通过以下方式填充第一个选择:
You can populate the first select this way:
<select>
<?php $c=count($array);
for ( $i=0; $i < $c; $i++)
{ ?>
<option><?php echo $array[$i]['name'];?></option>
<?php } ?>
</select>
第二次选择:
<select>
<?php
for ( $i=0; $i < $c; $i++)
{
$c2=count($array[$i]);
for ($j=0;$j<$c2;$j++){
?>
<option><?php echo $array[$i][$j]['name'];?></option>
<?php }} ?>
</select>
这篇关于多维数组-如何从子数组中获取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文