如何通过使用foreach循环通过这个php关联数组循环
问题描述:
$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关联数组循环?如何通过使用foreach循环通过这个php关联数组循环
答
你可以做一个双的foreach循环所有子阵列的所有值。
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($students as $name=>$student){
print $name . ":" . $student["class"] . PHP_EOL;
}
将输出这个
rishab:MCA
kamran:MBA
Sunil:MBA
谢谢主席先生!为即时回复。 – Abid
我很高兴我帮助你。如果答案适合您,请考虑将其标记为接受的答案。 –