To iterate over a Two Dimensional Array in PHP, you can use nested loops.
In this example,
$matrix
.<?php
$matrix = [[0, 1, 2], [1, 2, 3], [2, 3, 4]];
foreach ($matrix as $row) {
foreach ($row as $element) {
echo $element . "\n";
}
}
?>
0 1 2 1 2 3 2 3 4
In this example,
$matrix
.<?php
$matrix = [['Hello', 'World'], ['PHP', 'JavaScript']];
foreach ($matrix as $row) {
foreach ($row as $element) {
echo $element . "\n";
}
}
?>
Hello World PHP JavaScript
In this tutorial, we learned How to Iterate over a Two Dimensional Array in PHP language with well detailed examples.