How do you use a for loop to output a two-dimensional array in PHP?
In PHP, nested for loops can be used to output a two-dimensional array. Here is an example:
$twoDimensionalArray = array(
array("A", "B", "C"),
array("D", "E", "F"),
array("G", "H", "I")
);
for ($i = 0; $i < count($twoDimensionalArray); $i++) {
for ($j = 0; $j < count($twoDimensionalArray[$i]); $j++) {
echo $twoDimensionalArray[$i][$j] . " ";
}
echo "<br>";
}
The output result is:
A B C
D E F
G H I
In the example above, a two-dimensional array is first defined. Then, two nested for loops are used to iterate through the array. The outer loop is used to iterate through each one-dimensional array, while the inner loop is used to iterate through each element within the one-dimensional array. Within the inner loop, the indexes $i and $j are used to access the elements of the two-dimensional array, and the echo statement is used to output the values of the elements. After each row is outputted, a line break is inserted using the
tag.