It requires a little more thinking while working with loops in multidimensional arrays. Let’s see the situation here.
Say we got a two-dimensional array.
<?php
$students[0]["fname"]="John";
$students[0]["lname"]="Cena";
$students[1]["fname"]="Shawn";
$students[1]["lname"]="Patrick";
$students[1]["fname"]="John";
$students[1]["lname"]="doe";
for($outer=0;$outer<count($students);$outer++):
foreach($students[$outer] as $key => $value):
echo "\$students[$key][$innerkey] = ";
echo $students[$key][$innerkey]." ";
echo"<br>";
endforeach;
echo"<br>";
endfor;
// or we can use foreach as outer loop too.
foreach($students as $key => $innerArray):
foreach($innerArray as $innerkey => $value):
echo "\$students[$outer][$key] = ";
echo $students[$outer][$key];
echo"<br>";
endforeach;
echo"<br>";
endforeach;
?>
This will gives output:
$students[0][fname] = John
$students[0][lname] = Cena
$students[1][fname] = Shawn
$students[1][lname] = Patrick
$students[2][fname] = John
$students[2][lname] = doe
If you have any issue , let us know in the comment box.