+ 1

HOW TO LOOP through a multi dimensional array in PHP

$score = array("John" =>array("math" =>"50","English" =>"60"), "Sarah" =>array("math" =>"10","English" =>"20"); I want to echo out something like John had 50 in math and 60 in English

16th Dec 2017, 8:42 AM
Chinedu Chiaju
Chinedu Chiaju - avatar
1 Odpowiedź
+ 4
<?php /* HOW TO LOOP through a multi dimensional array in PHP */ $score = array("John" =>array("math" =>"50","English" =>"60"), "Sarah" =>array("math" =>"10","English" =>"20")); /* I want to echo out something like John had 50 in math and 60 in English */ foreach($score as $student => $scoredata) { echo "$student scores "; $i = 1; foreach($scoredata as $major => $score) { echo "$score in $major"; if($i) { echo " and "; $i--; } } echo "<br />"; } ?>
16th Dec 2017, 11:20 AM
Ipang