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. The following example steps through an associative array called $character, printing its key, the text has a value of, and the value, followed by an HTML break.

     while (list($key, $val) ...

Get Sams Teach Yourself PHP, MySQL® and Apache All in One now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.