As we discussed in Chapter 2, PHP supports both scalar and compound data types. In this chapter, we'll discuss one of the compound types: arrays. An
array is a collection of data values, organized as an ordered collection of key-value pairs.

This chapter talks about creating an array, adding and removing elements from an array, and looping over the contents of an array. There are many built-in functions that work with arrays in PHP, because arrays are very common and useful. For example, if you want to send email to more than one email address, you'll store the email addresses in an array and then loop through the array, sending the message to the current email address. Also, if you have a form that permits multiple selections, the items the user selected are returned in an array.Indexed Versus Associative Arrays

There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value.

PHP internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is what the keys happen to be. Some array features are provided mainly for use with indexed arrays, because they assume that you have or want keys that are consecutive integers beginning at 0. In both cases, the keys are unique--that is, you can't have two elements with the same key, regardless of whether the key is a string or an integer.PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. The order is normally that in which values were inserted into the array, but the sorting functions described later let you change the order to one based on keys, values, or anything else you choose.Identifying Elements of an Array

You can access specific values from an array using the array variable's name, followed by the element's key (sometimes called the
index) within square brackets:

$age['Fred']
$shows[2]

The key can be either a string or an integer. String values that are equivalent to integer numbers (without leading zeros) are treated as integers. Thus,
$array[3] and
$array['3'] reference the same element, but
$array['03'] references a different element. Negative numbers are valid keys, and they don't specify positions from the end of the array as they do in Perl.
You don't have to quote single-word strings. For instance,
$age['Fred'] is the same as
$age[Fred]. However, it's considered good PHP style to always use quotes, because quoteless keys are indistinguishable from constants. When you use a constant as an unquoted index, PHP uses the value of the constant as the index:

This construct assumes the array's indexes are numbers and assigns elements into the next available numeric index, starting from 0. Attempting to append to an associative array is almost always a programmer mistake, but PHP will give the new elements numeric indexes without issuing a warning:

To create an array initialized to the same value, use
array_pad( ). The first argument to
array_pad( ) is the array, the second argument is the minimum number of elements you want the array to have, and the third argument is the value to give any elements that are created. The
array_pad( ) function returns a new padded array, leaving its argument array alone.

You can refer to elements of multidimensional arrays by appending more
[]s:

$value = $multi[2][0]; // row 2, column 0. $value = 7

To interpolate a lookup of a multidimensional array, you must enclose the entire array lookup in curly braces:

echo("The value at row 2, column 0 is {$multi[2][0]}\n");

Failing to use the curly braces results in output like this:

The value at row 2, column 0 is Array[0]

Extracting Multiple Values

To copy all of an array's values into variables, use the
list( ) construct:

list(
$variable, ...) =
$array;

The array's values are copied into the listed variables, in the array's internal order. By default that's the order in which they were inserted, but the sort functions described later let you change that. Here's an example:

To extract only a subset of the array, use the
array_slice( ) function:

$subset = array_slice
(array,
offset,
length);

The
array_slice( ) function returns a new array consisting of a consecutive series of values from the original array. The
offset parameter identifies the initial element to copy (
0 represents the first element in the array), and the
length parameter identifies the number of values to copy. The new array has consecutive numeric keys starting at 0. For example:

To divide an array into smaller, evenly sized arrays, use the
array_chunk( ) function:

$chunks = array_chunk(
array,
size [,
preserve_keys]);

The function returns an array of the smaller arrays. The third argument,
preserve_keys, is a Boolean value that determines whether the elements of the new arrays have the same keys as in the original (useful for associative arrays) or new numeric keys starting from 0 (useful for indexed arrays). The default is to assign new keys, as shown here:

In PHP 4.0.6 and earlier versions, the
array_key_exists( ) function was called
key_exists( ). The original name is still retained as an alias for the new name.
Many people use the
isset( ) function instead, which returns
true if the element exists and is not
NULL:

PHP provides two functions,
extract( ) and
compact( ), that convert between arrays and variables. The names of the variables correspond to keys in the array, and the values of the variables become the values in the array. For instance, this array:

$person = array('name' => 'Fred', 'age' => 35, 'wife' => 'Betty');

can be converted to, or built from, these variables:

$name = 'Fred';
$age = 35;
$wife = 'Betty';

Creating Variables from an Array

The
extract( ) function automatically creates local variables from an array. The indexes of the array elements are the variable names:

extract($person); // $name, $age, and $wife are now set

If a variable created by the extraction has the same name as an existing one, the extracted variable overwrites the existing variable.You can modify
extract( )'s behavior by passing a second argument. Appendix A describes the possible values for this second argument. The most useful value is
EXTR_PREFIX_SAME, which says that the third argument to
extract( ) is a prefix for the variable names that are created. This helps ensure that you create unique variable names when you use
extract( ). It is good PHP style to always use
EXTR_PREFIX_SAME, as shown here:

The
compact( ) function is the complement of
extract( ). Pass it the variable names to compact either as separate parameters or in an array. The
compact( ) function creates an associative array whose keys are the variable names and whose values are the variable's values. Any names in the array that do not correspond to actual variables are skipped. Here's an example of
compact( ) in action:

The most common task with arrays is to do something with every element--for instance, sending mail to each element of an array of addresses, updating each file in an array of filenames, or adding up each element of an array of prices. There are several ways to traverse arrays in PHP, and the one you choose will depend on your data and the task you're performing.

The foreach Construct

The most common way to loop over elements of an array is to use the
foreach construct:

PHP executes the body of the loop (the
echo statement) once for each element of
$addresses in turn, with
$value set to the current element. Elements are processed by their internal order.
An alternative form of
foreach gives you access to the current key:

In this case, the key for each element is placed in
$k and the corresponding value is placed in
$v.
The
foreach construct does not operate on the array itself, but rather on a copy of it. You can insert or delete elements in the body of a
foreach loop, safe in the knowledge that the loop won't attempt to process the deleted or inserted elements.
The Iterator Functions

Every PHP array keeps track of the current element you're working with; the pointer to the current element is known as the
iterator. PHP has functions to set, move, and reset this iterator. The iterator functions are:

current( )

Returns the element currently pointed at by the iterator

reset( )

Moves the iterator to the first element in the array and returns it

next( )

Moves the iterator to the next element in the array and returns it

prev( )

Moves the iterator to the previous element in the array and returns it

end( )

Moves the iterator to the last element in the array and returns it

each( )

Returns the key and value of the current element as an array and moves the iterator to the next element in the array

key( )

Returns the key of the current element

The
each( ) function is used to loop over the elements of an array. It processes elements according to their internal order:

This approach does not make a copy of the array, as
foreach does. This is useful for very large arrays when you want to conserve memory.
The iterator functions are useful when you need to consider some parts of the array separately from others.
Example 5-1 shows code that builds a table, treating the first index and value in an associative array as table column headings.

If you know that you are dealing with an indexed array, where the keys are consecutive integers beginning at 0, you can use a
for loop to count through the indexes. The
for loop operates on the array itself, not on a copy of the array, and processes elements in key order regardless of their internal order.

PHP provides a mechanism,
array_walk( ), for calling a user-defined function once per element in an array:

array_walk(
array,
function_name);

The function you define takes in two or, optionally, three arguments: the first is the element's value, the second is the element's key, and the third is a value supplied to
array_walk( ) when it is called. For instance, here's another way to print table columns made of the values from an array:

A variation of this example specifies a background color using the optional third argument to
array_walk( ). This parameter gives us the flexibility we need to print many tables, with many background colors:

PHP automatically indexes the values in arrays, so
in_array( ) is much faster than a loop that checks every value to find the one you want.
Example 5-2 checks whether the user has entered information in all the required fields in a form.

The
array_search( ) function also takes the optional third
strict argument, which requires the types of the value being searched for and the value in the array to match.
Sorting

Sorting changes the internal order of elements in an array and optionally rewrites the keys to reflect this new order. For example, you might use sorting to arrange a list of scores from biggest to smallest, to alphabetize a list of names, or to order a set of users based on how many messages they posted.

PHP provides three ways to sort arrays--sorting by keys, sorting by values without changing the keys, or sorting by values and then changing the keys. Each kind of sort can be done in ascending order, descending order, or an order defined by a user-defined function.
Sorting One Array at a Time

The functions provided by PHP to sort an array are shown in
Table 5-1.

Table 5-1:
PHP functions for sorting an array

Effect

Ascending

Descending

User-defined order

Sort array by values, then reassign indexes starting with 0

sort( )

rsort( )

usort( )

Sort array by values

asort( )

arsort( )

uasort( )

Sort array by keys

ksort( )

krsort( )

uksort( )

The
sort( ),
rsort( ), and
usort( ) functions are designed to work on indexed arrays, because they assign new numeric keys to represent the ordering. They're useful when you need to answer questions like "what are the top 10 scores?" and "who's the third person in alphabetical order?" The other sort functions can be used on indexed arrays, but you'll only be able to access the sorted ordering by using traversal functions such as
foreach and
next.
To sort names into ascending alphabetical order, you'd use this:

To get them in reverse alphabetic order, simply call
rsort( ) instead of
sort( ).
If you have an associative array mapping usernames to minutes of login time, you can use
arsort( ) to display a table of the top three, as shown here:

User-defined ordering requires that you provide a function that takes two values and returns a value that specifies the order of the two values in the sorted array. The function should return
1 if the first value is greater than the second,
-1 if the first value is less than the second, and
0 if the values are the same for the purposes of your custom sort order.
Example 5-3 is a program that lets you try the various sorting functions on the same data.

PHP's built-in sort functions correctly sort strings and numbers, but they don't correctly sort strings that contain numbers. For example, if you have the filenames
ex10.php,
ex5.php, and
ex1.php, the normal sort functions will rearrange them in this order:
ex1.php,
ex10.php,
ex5.php. To correctly sort strings that contain numbers, use the
natsort( ) and
natcasesort( ) functions:

Pass it a series of arrays and sorting orders (identified by the
SORT_ASC or
SORT_DESC constants), and it reorders the elements of all the arrays, assigning new indexes. It is similar to a join operation on a relational database.
Imagine that you have a lot of people, and several pieces of data on each person:

The first element of each array represents a single record--all the information known about Tom. Similarly, the second element constitutes another record--all the information known about Dick. The
array_multisort( ) function reorders the elements of the arrays, preserving the records. That is, if
Dick ends up first in the
$names array after the sort, the rest of Dick's information will be first in the other arrays too. (Note that we needed to quote Dick's zip code to prevent it from being interpreted as an octal constant.)
Here's how to sort the records first ascending by age, then descending by zip code:

array_multisort($ages, SORT_ASC, $zips, SORT_DESC, $names, SORT_ASC);

We need to include
$names in the function call to ensure that Dick's name stays with his age and zip code. Printing out the data shows the result of the sort:

The
array_reverse( ) function reverses the internal order of elements in an array:

$reversed = array_reverse(
array);

Numeric keys are renumbered starting at 0, while string indexes are unaffected. In general, it's better to use the reverse-order sorting functions instead of sorting and then reversing the order of an array.
The
array_flip( ) function returns an array that reverses the order of each original element's key-value pair:

$flipped = array_flip(
array);

That is, for each element of the array whose value is a valid key, the element's value becomes its key and the element's key becomes its value. For example, if you have an array mapping usernames to home directories, you can use
array_flip( ) to create an array mapping home directories to usernames:

Elements whose original values are neither strings nor integers are left alone in the resulting array. The new array lets you discover the key in the original array given its value, but this technique works effectively only when the original array has unique values.
Randomizing Order

To traverse the elements in an array in a random order, use the
shuffle( ) function. All existing keys, whether string or numeric, are replaced with consecutive integers starting at 0.

Obviously, the order after your
shuffle( ) may not be the same as the sample output here. Unless you are interested in getting multiple random elements from an array, without repeating any specific item, using the
rand( ) function to pick an index is more efficient.
Acting on Entire Arrays

PHP has several useful functions for modifying or applying an operation to all elements of an array. You can merge arrays, find the difference, calculate the total, and more, all using built-in functions.

Calculating the Sum of an Array

The
array_sum( ) function adds up the values in an indexed or associative array:

Values are compared using
===, so
1 and
"1" are considered different. The keys of the first array are preserved, so in
$diff the key of
'bill' is 0 and the key of
'judy' is 4.
Filtering Elements from an Array

To identify a subset of an array based on its values, use the
array_filter( ) function:

$filtered = array_filter(
array,
callback);

Each value of
array is passed to the function named in
callback. The returned array contains only those elements of the original array for which the function returns a
true value. For example:

As you see, the keys are preserved. This function is most useful with associative arrays.
Using Arrays

Arrays crop up in almost every PHP program. In addition to their obvious use for storing collections of values, they're also used to implement various abstract data types. In this section, we show how to use arrays to implement sets and stacks.

Sets

Arrays let you implement the basic operations of set theory: union, intersection, and difference. Each set is represented by an array, and various PHP functions implement the set operations. The values in the set are the values in the array--the keys are not used, but they are generally preserved by the operations.

The
union of two sets is all the elements from both sets, with duplicates removed. The
array_merge( ) and
array_unique( ) functions let you calculate the union. Here's how to find the union of two arrays:

The
intersection of two sets is the set of elements they have in common. PHP's built-in
array_intersect( ) function takes any number of arrays as arguments and returns an array of those values that exist in each. If multiple keys have the same value, the first key with that value is preserved.
Another common function to perform on a set of arrays is to get the
difference; that is, the values in one array that are not present in another array. The
array_diff( ) function calculates this, returning an array with values from the first array that are not present in the second.
The following code takes the difference of two arrays:

Although not as common in PHP programs as in other programs, one fairly common data type is the last-in first-out (LIFO) stack. We can create stacks using a pair of PHP functions,
array_push( ) and
array_pop( ). The
array_push( ) function is identical to an assignment to
$array[]. We use
array_push( ) because it accentuates the fact that we're working with stacks, and the parallelism with
array_pop() makes our code easier to read. There are also
array_shift( ) and
array_unshift( ) functions for treating an array like a queue.

Stacks are particularly useful for maintaining state.
Example 5-4 provides a simple state debugger that allows you to print out a list of which functions have been called up to this point (i.e., the
stack trace).

Entering first (stack is now: first)
Exiting
Entering third (stack is now: third)
Entering second (stack is now: third -> second)
Entering first (stack is now: third -> second -> first)
Exiting
Exiting
Entering first (stack is now: third -> first)
Exiting
Exiting