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

Open Source Technologies



                   Notes         The output of this example is
                                 #0 = Peter

                                 #1 = Barbara

                                 #2 = Bill
                                 #3 = Nancy

                                 7.8.8 Reset( )

                                 Iteration in PHP is done by using an internal array pointer that keeps record of the current
                                 position of the traversal. Unlike with foreach(), when you want to use each() to iterate over an
                                 array, you must reset() the array before you start to iterate over it. In general, it is best for you
                                 to always use foreach() and not deal with this subtle nuisance of each() traversal.
                                 7.8.9 Each( )

                                 The each( ) function returns the current key/value pair and advances the internal pointer to the
                                 next element. When it reaches the end of of the array, it returns a booloean value of false. The
                                 key/value pair is returned as an array with four elements: the elements 0 and “key”, which
                                 have the value of the key, and elements 1 and “value”, which have the value of the value. The
                                 reason for duplication is that, if you’re accessing these elements individually, you’ll probably
                                 want to use the names such as
                                 $elem[“key”] and $elem[“value”]:

                                 $ages = array(“Peter” => 28, “Barbara” => 67);
                                 reset($ages);

                                 $person = each($ages);
                                 print $person[“key”];

                                 print “ is of age “;
                                 print $person[“value”];

                                 This prints
                                 Peter is of age 28

                                 When we explain how the list() construct works, you will understand why offsets 0 and 1 also
                                 exist.
                                 7.8.10 List( )
                                 The list( ) construct is a way of assigning multiple array offsets to multiple variables in one
                                 statement:
                                 list($var1, $var2, ...) = $array;
                                 The first variable in the list is assigned the array value at offset 0, the second is assigned offset 1,
                                 and so on. Therefore, the list() construct translates into the following series of PHP statements:
                                 $var1 = $array[0];




        118                               LOVELY PROFESSIONAL UNIVERSITY
   118   119   120   121   122   123   124   125   126   127   128