Arrays

An array is a special kind of variable that lets you store many individual pieces of data. PHP provides many functions for dealing with arrays, such as counting, sorting, and looping through the array data.

Elements are the data values that are stored in the array. Each element can be referenced by its unique array index (or key). The index may be a string or a number.

Associative Versus Numeric Indexed Arrays
Numeric arrays use numbers as indexes. PHP will automatically assign a numeric index, starting at 0, when you add an element to a numeric array. An example numeric array of colors:

Associative arrays use strings. You must supply an index string when you add elements to an associative array. This example associates household objects with their shapes:

An array's elements can be any type of value, including objects and other arrays.

An array's indexes must be scalar (simple, not compound or complex) values such as numbers, strings, or booleans.

Assignment
Array identifiers (names) are followed by square brackets, both to distinguish them from other kinds of variables and to suggest that arrays are indexed. The following code segment assigns Monday and Tuesday to the first two elements of the numeric array named $weekday:  Note that if you assign indexes yourself, be careful not to skip an index--PHP will NOT report this as an error.

Assignment Using Array
You can also use the array language construct to assign values to an array:  'Cylinder',               'Note Pad' => 'Rectangle',                'Apple' => 'Sphere',                'Orange' => 'Sphere'); ?>

To test whether a variable is an array, use the is_array function: 

Looping Through an Array
We can access individual array elements through their indexes either individually or by using a loop:  $value) { // An associative array has key/value pairs. print "The $key is a $value. "; } ?>

Working With Arrays
 $value) { echo "fruits[". $key. "] = " . $value. " "; } ?> The output from this segment would be: fruits[0] = apple fruits[1] = banana fruits[2] = cherry fruits[3] = orange

Examples
Here are some array processing examples.