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

Unit 7: Multidimensional Arrays



                 (c)   Remove or insert elements in an array                                      Notes
                 (d)   None of these.
            True or False:

               7.  The extract( ) function automatically creates local variables from an array.
                 (a)   True                      (b)  False

               8.  array_filter( ) function to identify a subset of an array based on its values.
                 (a)   True                      (b)  False

            7.8 Summary

               •  Array does not have to be a simple list of keys and values; each array element can contain
                 another array as a value, which in turn can hold other arrays as well.
               •  One-dimensional array is enough to keep the two types elements. But if you need to keep
                 more than one item of each type you need to use something different one of the ways to
                 do it is using multidimensional arrays.
               •  PHP provides two functions, extract ( ) and compact ( ), that convert between arrays and
                 variables. The names of the variables correspond to keys in the array, and the values of
                 the variables become the values in the array.
               •  Every PHP array keeps track of the current element you’re working with; the pointer to
                 the current element is known as the iterator. PHP has functions to set, move, and reset
                 this iterator.
               •  Sorting arrays with more than one dimension, or by something other than alphabetical or
                 numerical order, is more complicated. PHP knows how to compare two numbers or two
                 text strings, but in a multidimensional array, each element is an array.
               •  The functions sort (), asort(), and ksort() all have a matching reverse sort with an “r” in
                 the function name. The user-defined sorts do not have reverse variants, but you can sort
                 a multidimensional array into reverse order.

            7.9 Keywords


            array_keys( ): The array_keys( ) function returns an array consisting of only the keys in the
            array, in internal order.
            array_slice( ): The array_slice( ) function returns a new array consisting of a consecutive series
            of values from the original array.
            array_splice( ): The array_splice( ) function can remove or insert elements in an array.
            each( ): Returns the key and value of the current element as an array and moves the iterator to
            the next element in the array.
            extract( ): The extract( ) function automatically creates local variables from an array. The indexes
            of the array elements are the variable names.
            Multidimensional array: A multidimensional array is an array that contains at least one other
            array as the value of one of the indexes.

                          1.  Develop a PHP program to create a two dimensional array.

                          2.  Develop a PHP program to add two metrics.



                                             LOVELY PROFESSIONAL UNIVERSITY                                   169
   170   171   172   173   174   175   176   177   178   179   180