2014-11-02 121 views
2

這是我的PHP代碼:的foreach在PHP在多維陣列循環

$marks = array(
    'Mohammad' => array('Phisics' => 50, 'Math' => 80), 
    'Arif'  => array('Phisics' => 55, 'Math' => 95), 

);

現在我想用主題使用foreach循環來獲得Mohammad和Arif的標記。

回答

0

您可以使用嵌套的循環foreach

foreach ($marks as $name => $subjects) { 
    foreach ($subjects as $subjectName => $subjectMark) { 
     echo "${name}'s mark for ${subjectName} is ${subjectMark}."; 
    } 
} 
+0

非常感謝Mureinik – 2014-11-02 18:49:19

0

試試這個

$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 
+0

謝謝你對我的幫助阿西克 – 2014-11-02 18:49:41