PHP Multidimensional Arrays

PHP multidimensional array is an array containing one or more arrays.

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.

in this example demonstrates this. we have an array called 'food', which contains 3 arrays (called 'fruit', 'vegetables', 'grains'). each of these arrays contain their own arrays.

   <?php
     
          $food = array(
          
                               "fruit" => array( "apples",
                                                     
                                                      "bananas",
                                                      
                                                      "oranges"
                                                   ),
                                                                            
                               "vegetables" => array(
                                                                "carrots",
                                                                                        
                                                                "potatoes"
                                                                                        
                                                            ),
                                                                                      
                               "grains" => array(
                                                                
                                                                "oatbran"
                                                      ));
    ?>

gets try to access a single value from the array above:

      echo "the third fruit =". $food["fruit"][2];                        //output oranges

      echo "the second vegetable =". $food["vegetables"][1];    //output potatoes

The following example demonstrates a foreach loop that will access multi-dimensional arrays:

   <?php
     
          $food = array(
          
                               "fruit" => array( "apples",
                                                     
                                                      "bananas",
                                                      
                                                      "oranges"
                                                   ),
                                                                            
                               "vegetables" => array(
                                                                "carrots",
                                                                                        
                                                                "potatoes"
                                                                                        
                                                            ),
                                                                                      
                               "grains" => array(
                                                                
                                                                "oatbran"
                                                      ));
   
   
    foreach( $food  as $key => $value){
         
           foreach( $value   as  $keys => $output){
                            
                      echo  "array key[".$key."]"."[". $keys ."] value  = ". $output;
                                  
                      echo  "<br/>";
                                  
                   }
                                
                }
    ?>

the following example demonstrates a foreach loop that will access two-dimensional arrays:

 <?php      
          $array = array();
               
                                 $array[0][0] = "one";
                               
                                 $array[0][1] = "two";
                               
                                 $array[0][2] = "three";  //array initialization statements
                               
                                 $array[1][0] = "four";
                               
                                 $array[1][1] = "five";
                               
                                 $array[1][2] = "six";
                               
                                 $array[2][0] = "seven";
                               
                                 $array[2][1] = "eight";
                               
                                 $array[2][2] = "nine";
                               
 foreach( $array   as   $keyi => $valuef ){
        
            foreach( $valuef  as  $keyj => $values ){
            
      echo  "array  key [". $keyi . "]" . "[" . $keyj . "] " . " value = " . $values ;                                   
                                   
      echo  "<br/>";  
      
   }
   
 }
 
 ?>