使用 foreach 循环遍历关联数组的关联数组 [英] Loop through associative array of associative arrays using foreach loop
本文介绍了使用 foreach 循环遍历关联数组的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$students = array(
'rishab' => array(
'age' =>25 ,
'marks' =>400,
'class' =>'MCA'
),
'kamran' => array(
'age' =>23 ,
'marks' =>550,
'class' =>'MBA'
),
'Sunil' => array(
'age' =>23 ,
'marks' =>550,
'class' =>'MBA'
)
);
如何使用 foreach 循环遍历这个 php 关联数组?
how can i loop through this php associative array using foreach loop??
推荐答案
你可以做一个双 foreach 来循环所有子数组的所有值.
You can make a double foreach to loop all values of all sub arrays.
foreach($students as $key => $value) {
echo 'Key: '.$key.'<br />';
foreach($value as $s_key => $s_value) {
echo 'Sub key: '.$s_key.' => '.$s_value.'<br />';
}
echo '<br />';
}
结果:
Key: rishab
Sub key: age => 25
Sub key: marks => 400
Sub key: class => MCA
Key: kamran
Sub key: age => 23
Sub key: marks => 550
Sub key: class => MBA
Key: Sunil
Sub key: age => 23
Sub key: marks => 550
Sub key: class => MBA
这篇关于使用 foreach 循环遍历关联数组的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文