How can the foreach loop be utilized to iterate through a two-dimensional array in PHP?
To iterate through a two-dimensional array in PHP using a foreach loop, you can nest two foreach loops. The outer loop iterates through the rows of the array, and the inner loop iterates through the columns of each row. Code snippet:
$twoDArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
foreach ($twoDArray as $row) {
foreach ($row as $value) {
echo $value . " ";
}
echo "\n";
}