這是我的PHP代碼:的foreach在PHP在多維陣列循環
$marks = array(
'Mohammad' => array('Phisics' => 50, 'Math' => 80),
'Arif' => array('Phisics' => 55, 'Math' => 95),
);
現在我想用主題使用foreach循環來獲得Mohammad和Arif的標記。
這是我的PHP代碼:的foreach在PHP在多維陣列循環
$marks = array(
'Mohammad' => array('Phisics' => 50, 'Math' => 80),
'Arif' => array('Phisics' => 55, 'Math' => 95),
);
現在我想用主題使用foreach循環來獲得Mohammad和Arif的標記。
您可以使用嵌套的循環foreach
:
foreach ($marks as $name => $subjects) {
foreach ($subjects as $subjectName => $subjectMark) {
echo "${name}'s mark for ${subjectName} is ${subjectMark}.";
}
}
試試這個
$marks = array(
'Mohammad' => array('Phisics' => 50, 'Math' => 80),
'Arif' => array('Phisics' => 55, 'Math' => 95),
);
foreach ($marks as $key=>$names) {
echo "--$key Marks-- <br/>";
foreach ($names as $key=>$value) {
echo "$key:$value <br/>";
}
}
輸出
--Mohammad Marks--
Phisics:50
Math:80
--Arif Marks--
Phisics:55
Math:95
謝謝你對我的幫助阿西克 – 2014-11-02 18:49:41
非常感謝Mureinik – 2014-11-02 18:49:19