php – 多维数组 – 如何从子数组中获取特定值
发布时间:2020-12-13 18:12:17 所属栏目:PHP教程 来源:网络整理
导读:我有以下数组结构: 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
我有以下数组结构:
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]值时迷失了方向. 我想要做的是使用下拉选择填充表单,基于第一级[名称](如[name] => Masura S),然后第二个下拉选择第二级[名称](如[名称] ] => Alb). 如果您有任何指示,我将不胜感激……
你可以用这种方式填充第一个选择:
<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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |