Students Empire

Learn Something New
Home

Learn 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.


              <?php 
              $myBooks = array( 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
              ); 

              print_r ( $myBooks ); 
            ?>  
            

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[][]


              <?php 
              $myBooks = array( 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
              ); 

              print_r( $myBooks[1] ); 
              echo $myBooks[1]["title"] . "\n"; 
              echo $myBooks[3]["pubYear"] . "\n"; 
            ?>  
            

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 ) { 

               } 
            } 
            

              <?php
               $myBooks = array( 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
                array( 
                  "title" =>  "Learn PHP", 
                  "author" =>  "studentsempire.com", 
                  "pubYear" =>  2018 
                ), 
              ); 

                $bookNum = 0; 

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