Page 171 - Beginning PHP 5.3
P. 171

Chapter 6: Arrays






































                           Figure 6-8


                         How It Works
                         This script demonstrates four different uses of array_splice(), displaying the results in an HTML
                         table. The first example inserts two new elements at the third position in the array, displaying the
                         removed elements, which in this case is an empty array because no elements were removed:

                             print_r( array_splice( $authors, 2, 0, $arrayToAdd ) );
                         You can read this line as: “At the third position (2), remove zero (0) elements, then insert
                         $arrayToAdd”.
                         The second example demonstrates how to remove and insert elements at the same time:
                             print_r( array_splice( $authors, 0, 2, $arrayToAdd ) );
                         This code removes two elements from the start of the array (position 0), then inserts the contents of
                         $arrayToAdd at position 0.
                         The third example shows what happens if you omit the third argument:
                             print_r( array_splice( $authors, 1 ) );




                                                                                                         133





                                                                                                      9/21/09   9:00:21 AM
          c06.indd   133
          c06.indd   133                                                                              9/21/09   9:00:21 AM
   166   167   168   169   170   171   172   173   174   175   176