I need to print the student name and the mark from the following array for a given subject:
$marks = [
"john" => ["physics" => 30, "maths" => 55, "chemistry" => 66],
"jack" => ["physics" => 44, "maths" => 19, "chemistry" => 87],
"mark" => ["physics" => 77, "maths" => 66, "chemistry" => 67],
];
I understand that if I do echo $marks['john']['chemistry'];
it will print the mark for the student/subject, but how should I approach a foreach loop for displaying all students and their scores for chemistry?
In php foreach()
you can get key of current item like this
foreach ($array as $key=>$item){...}
Also use it like bottom code
foreach ($marks as $name=>$scores){
echo $name .":". $scores["chemistry"];
}
See result of code in demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With