PHP Tutorial - PHP Array Multidimensional






A multi-dimensional array can have array as its value.

An array that contains other arrays is a two-dimensional array. If those arrays also contain arrays, then the top-level array is a three-dimensional array, and so on.

The following code uses array operator ([]) to create an two-dimensional array. The key of the first level is Java and PHP.


<?PHP
$book['Java'] = array("Name"=>"Java Book", "Price"=> 1.2, "OnSale"=>"No");
$book['PHP'] = array("Name"=>"PHP Book", "Price"=>5.3, "OnSale"=>"Yes");
var_dump($book);
?>

The code above generates the following result.





Example - Create two-dimensional array with array() function

The following script creates a simple two-dimensional array called $myBooks , then displays its contents using print_r().


<?php /*  ww w  .j  a va2 s.  co  m*/
  $myBooks = array( 
    array( 
      "title" =>  "Learn PHP from java2s.com", 
      "author" =>  "java2s.com", 
      "pubYear" =>  2000 
    ), 
    array( 
      "title" =>  "Learn Java from java2s.com", 
      "author" =>  "JavaAuthor", 
      "pubYear" =>  2001 
    ), 
    array( 
      "title" =>  "Learn HTML from java2s.com", 
      "author" =>  "HTMLAuthor", 
      "pubYear" =>  2002 
    ), 
    array( 
      "title" =>  "Learn CSS from java2s.com", 
      "author" =>  "CSSAuthor", 
      "pubYear" =>  2003 
    ), 
  ); 

  print_r ( $myBooks ); 
?>  

The code above generates the following result.





PHP Access Element in Multidimensional Array

Square bracket syntax can access any element within a multidimensional array.

To access the first dimension

$multidimensionalArray[]

To access the inner dimension

$multidimensionalArray[][]

Here are some examples


<?php /*from w  ww  . j ava2  s  .  c o  m*/
  $myBooks = array( 
    array( 
      "title" =>  "Learn PHP from java2s.com", 
      "author" =>  "java2s.com", 
      "pubYear" =>  2000 
    ), 
    array( 
      "title" =>  "Learn Java from java2s.com", 
      "author" =>  "JavaAuthor", 
      "pubYear" =>  2001 
    ), 
    array( 
      "title" =>  "Learn HTML from java2s.com", 
      "author" =>  "HTMLAuthor", 
      "pubYear" =>  2002 
    ), 
    array( 
      "title" =>  "Learn CSS from java2s.com", 
      "author" =>  "CSSAuthor", 
      "pubYear" =>  2003 
    ), 
  ); 
  print_r( $myBooks[1] ); 
  echo $myBooks[1]["title"] . "\n"; 
  echo $myBooks[3]["pubYear"] . "\n";   
?>

The code above generates the following result.

PHP Loop Through Multidimensional Array

Multidimensional arrays are basically arrays nested inside other arrays, we can loop through multidimensional arrays using nested loops!

We can use the following nested foreach statements to loop through multidimensional array.

foreach ( $myBooks as $book ) { 
   foreach ( $book as $key => $value ) { 

   } 
} 

The following example uses two nested foreach loops to loop through the $myBooks array.


<?php /* w  w w. java2s  .  c  o  m*/
   $myBooks = array( 
     array( 
       "title" => "Learn PHP from java2s.com", 
       "author" => "java2s.com", 
       "pubYear" => 2000 
     ), 
     array( 
       "title" => "Learn Java from java2s.com", 
       "author" => "JavaAuthor", 
       "pubYear" => 2001 
     ), 
     array( 
       "title" => "Learn HTML from java2s.com", 
       "author" => "HTMLAuthor", 
       "pubYear" => 2002 
     ), 
     array( 
       "title" => "Learn CSS from java2s.com", 
       "author" => "CSSAuthor", 
       "pubYear" => 2003 
     ), 
    ); 

    $bookNum = 0; 

    foreach ( $myBooks as $book ) { 
     $bookNum++; 
     echo "Book #$bookNum:"; 
     foreach ( $book as $key => $value ) { 
       echo "$key :$value \n"; 
     } 
    } 
 ?> 

The code above generates the following result.