Page 153 - Beginning PHP 5.3
P. 153

Chapter 6: Arrays
                           Now you can rewrite the example that used  each()  with a  while  loop in the previous section
                          ( “ Stepping Through an Array ” ) to use a   foreach  loop instead:
                               < !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
                               < html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
                                < head >
                                  < title > Using foreach < /title >
                                  < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                                < /head >
                                < body >
                                  < h1 > Using foreach < /h1 >

                                  < dl >
                               < ?php

                             $myBook = array( “title” = >  “The Grapes of Wrath”,
                                              “author” = >  “John Steinbeck”,
                                              “pubYear” = >  1939 );
                             foreach ( $myBook as $key = >  $value ) {
                               echo “ < dt > $key < /dt > ”;
                               echo “ < dd > $value < /dd > ”;
                             }
                             ? >

                                  < /dl >
                                < /body >
                               < /html >
                           This code produces the same list of keys and values as shown in Figure  6-3 .


                           Altering Array Values with foreach
                           When using  foreach , the values you work with inside the loop are copies of the values in the array
                         itself. This means that if you change the value given to you by   foreach , you ’ re not affecting the
                          corresponding value in the original array. The following example code illustrates this:

                             $authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
                             // Displays “Steinbeck Kafka Hardy Dickens”;
                             foreach ( $authors as $val ) {
                               if ( $val == “Tolkien” ) $val = “Hardy”;
                               echo $val . “ “;
                             }
                             echo “ < br/ > ”;


                             // Displays “Array ( [0] = >  Steinbeck [1] = >  Kafka [2] = >  Tolkien [3] = >
                             Dickens )”

                             print_r ( $authors );
                                                                                                         115





                                                                                                      9/21/09   9:00:14 AM
          c06.indd   115                                                                              9/21/09   9:00:14 AM
          c06.indd   115
   148   149   150   151   152   153   154   155   156   157   158