Tags
Asked 3 years ago
9 Jun 2021
Views 240
jagdish

jagdish posted

php multidimensional array

how to use multidimensional array in php ?
how to intialize multidimensional array in php ?
shyam

shyam
answered Apr 26 '23 00:00

In PHP, a multidimensional array is an array that contains one or more arrays as its elements. Here's an example of how to declare and use a multidimensional array in PHP:



// Declare a multidimensional array
$myArray = array(
  array(1, 2, 3),
  array(4, 5, 6),
  array(7, 8, 9)
);

// Access an element in the multidimensional array
echo $myArray[1][2]; // Output: 6

// Loop through a multidimensional array
foreach ($myArray as $row) {
  foreach ($row as $col) {
    echo $col . ' ';
  }
  echo '<br>';
}
// Output:
// 1 2 3
// 4 5 6
// 7 8 9

In this example, we declare a multidimensional array $myArray that contains three sub-arrays, each with three elements. We access an element in the array using two indices, one for the row and one for the column. We also loop through the array using two foreach loops, one for the rows and one for the columns.
Post Answer