Display Two-Dimensional Array with Nested Loops in PHP



Use two loops, one for row and another for column. Fr matrix form, use the tab \t in the nested loop like his −

twoDimensionalArray[row][col],"\t";

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$twoDimensionalArray = [];
$value=7;
for ($row = 0; $row <=2; $row++) {
   for ($col = 0; $col <=2; $col++) {
      $twoDimensionalArray[row][col] = $value;
   }
}
for ($row = 0; $row <=2; $row++) {
   for ($col = 0; $col <=2; $col++) {
      echo $twoDimensionalArray[row][col],"\t";
   }
   echo "<br>";
}
?>
</body>
</html>

Output

777
777
777

Above, you can see the output is in matrix form 3x3.

Updated on: 2020-10-12T13:07:09+05:30

359 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements