Page 142 - Beginning PHP 5.3
P. 142

Part II: Learning the Language
                  Changing Elements

                   As well as accessing array values, you can also change values using the same techniques. It ’ s helpful to
                 think of an array element as if it were a variable in its own right; you can create, read, and write its
                 value at will.

                   For example, the following code changes the value of the third element in an indexed array from
                     “ Tolkien ”   to   “ Melville “  :

                    $authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
                    $authors[2] = “Melville”;

                   What if you wanted to add a fifth author? You can just create a new element with an index of 4,
                 as follows:

                    $authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
                    $authors[4] = “Orwell”;

                   There ’ s an even easier way to add a new element to an array  —  simply use square brackets with
                 no index:

                    $authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );

                    $authors[] = “Orwell”;
                   When you do this, PHP knows that you want to add a new element to the end of the array, and it
                 automatically assigns the next available index  —  in this case,   4  —  to the element.

                   In fact, you can create an array from scratch simply by creating its elements using the square bracket
                 syntax. The following three examples all produce exactly the same array:

                    // Creating an array using the array() construct
                    $authors1 = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );

                    // Creating the same array using [] and numeric indices
                    $authors2[0] = “Steinbeck”;
                    $authors2[1] = “Kafka”;
                    $authors2[2] = “Tolkien”;
                    $authors2[3] = “Dickens”;

                    // Creating the same array using the empty [] syntax
                    $authors3[] = “Steinbeck”;
                    $authors3[] = “Kafka”;
                    $authors3[] = “Tolkien”;
                    $authors3[] = “Dickens”;

                   However, just as with regular variables, you should make sure your arrays are initialized properly first.
                 In the second and third examples, if the   $authors2  or  $authors3  array variables already existed and
                contained other elements, the final arrays might end up containing more than just the four elements
                you assigned.





              104





                                                                                                      9/21/09   9:00:09 AM
          c06.indd   104                                                                              9/21/09   9:00:09 AM
          c06.indd   104
   137   138   139   140   141   142   143   144   145   146   147