Some Array-Related Functions


Approximately 60 array-related functions are built into PHP, which you can read about in detail at http://www.php.net/array. Some of the more common (and useful) functions are explained in this section.

  • count() and sizeof() Each counts the number of elements in an array. Given the following array

     $colors = array("blue", "black", "red", "green"); 

    both count($colors); and sizeof($colors); return a value of 4.

  • each() and list() These usually appear together, in the context of stepping through an array and returning its keys and values. You saw an example of this above, where we stepped through the $c array and printed its contents.

  • foreach() This is also used to step through an array, assigning the value of an element to a given variable, as you saw in the previous section.

  • reset() This rewinds the pointer to the beginning an array, as in this example:

     reset($character); 

    This function is useful when you are performing multiple manipulations on an array, such as sorting, extracting values, and so forth.

  • array_push() This adds one or more elements to the end of an existing array, as in this example:

     array_push($existingArray, "element 1", "element 2", "element 3"); 

  • array_pop() This removes (and returns) the last element of an existing array, as in this example:

     $last_element = array_pop($existingArray); 

  • array_unshift() This adds one or more elements to the beginning of an existing array, as in this example:

     array_unshift($existingArray, "element 1", "element 2", "element 3"); 

  • array_shift() This removes (and returns) the first element of an existing array, as in this example, where the value of the element in the first position of $existingArray is assigned to the variable $first_element:

     $first_element = array_shift($existingArray); 

  • array_merge() This combines two or more existing arrays, as in this example:

     $newArray = array_merge($array1, $array2); 

  • array_keys() This returns an array containing all the key names within a given array, as in this example:

     $keysArray = array_keys($existingArray); 

  • array_values() This returns an array containing all the values within a given array, as in this example:

     $valuesArray = array_values($existingArray); 

  • shuffle() This randomizes the elements of a given array. The syntax of this function is simply as follows:

     shuffle($existingArray); 

This brief rundown of array-related functions only scratches the surface of using arrays. However, arrays and array-related functions are used in the code examples throughout this book. Next, we will move on to discussing objects, but only briefly as it is a subject worthy of an entire book, and we will not be using objects throughout the procedural scripts shown in the remainder of this book.



Sams Teach Yourself PHP MySQL and Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (4th Edition)
ISBN: 067232976X
EAN: 2147483647
Year: 2003
Pages: 333
Authors: Julie Meloni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net