Page 157 - Open Soource Technologies 304.indd
P. 157

Unit 7: Multidimensional Arrays



                           array( ‘TRK_SPK’, ‘Spark Plugs’, 6 )                                   Notes
                          )
                      );

            Because this array has only numeric indices, we can use nested for loops to display its contents.
            for ( $layer = 0; $layer < 3; $layer++ )

            {
             echo “Layer $layer<br />”;
             for ( $row = 0; $row < 3; $row++ )
             {

              for ( $column = 0; $column < 3; $column++ )
              {

               echo ‘|’.$categories[$layer][$row][$column];
              }
              echo ‘|<br />’;
             }

            }
            Because of the way multidimensional arrays are created, we could create four-, five-, or six-
            dimensional arrays. There is no language limit to the number of dimensions, but it is difficult
            for people to visualize constructs with more than three dimensions. Most real-world problems
            match logically with constructs of three or four dimensions.

            Self Assessment

            Choose the correct answer:
               1.  The correct definition of an array:
                 (a)   Array is the collection of elements.
                 (b)   Array is the list of elements.
                 (c)   Array is the collection of elements with contiguous memory allocation.
                 (d)   None of these.

               2.  Each array element can contain another array as a value.
                 (a)   True                      (b)  False

               3.  The list ( ) construct is use to:
                 (a)   Delete all the elements in array
                 (b)   Copy all of an array’s values into variables

                 (c)   Create an array
                 (d)   None of these.







                                             LOVELY PROFESSIONAL UNIVERSITY                                   151
   152   153   154   155   156   157   158   159   160   161   162