Computerworld

A closer look at arrays in PHP

Over previous tutorials, we have occasionally made use of arrays without going into much detail about exactly what arrays should be used for, which kind of arrays are supported by PHP and the routines the language provides to simplify array usage.

Python vs. PHP: Choosing your next project's language

What are arrays?

Generally speaking, an array is a data set, containing one or more members. It is a useful data structure, in as much as it allows related information to be stored in a single variable. For example, say a PHP script has to manipulate information concerning a user's username, e-mail address, first name and last name. This information can be stored in four variables. But what if the script has to manipulate the data for 1000 users, in the same script? That would require 4000 variables! By storing all the user data in an array, such a script can be greatly simplified.

Arrays in PHP have another property: they are a dynamic data structure. That is, unlike an integer which can have a maximum value of 2147483647 in PHP, arrays can (theoretically) have an infinite number of members.

Arrays in PHP

Arrays can be created in several ways in PHP. For example:

01 <?
02 $a = array(1,2);
03 ?>

This creates an array with two members: 1 and 2. The following is equivalent.

01 <?
02 $a = array();
03 $a[] = 1;
04 $a[] = 2;
05 ?>

PHP also supports associative arrays. Such arrays allow developers to associate a unique name with any given member of an array. Consider the following:

01 <?
02 $a = array("username"=>"gavin","email"=>"gavin@my.host.com",
03		"firstname"=>"Gavin","lastname"=>"Sherry");
04 echo "Username: {$a["username"]}\n";
05 ?>

Lines 02 and 03 declare an associative array $a, containing information about a user. Keys, such as 'username' and 'email', are related to the values in the array, allowing data to be referenced by an associative name. Line 04 outputs the value of the username, referring to it in the array by name.

Notice that when a specific value in an array is referenced by the echo statement, it must be surrounded by curly brackets {}, otherwise PHP takes it to be a literal.

Basic array manipulation in PHP

PHP provides many functions to allow developers to get the most out of arrays. At their most basic level, these functions allow values to be added to arrays, removed from them and for the number of members in an array to be returned. Consider the following example.

01 <?
02 $a = array();
03 array_push($a,"apples");
04 array_push($a,"oranges","bananas");
05 while(list($key,$val) = each($a)) {
06 echo "key = $key, val = $val\n";
07 }
08 reset($a);
09 array_pop($a);
10 while(list($key,$val) = each($a)) {
11 echo "key = $key, val = $val\n";
12 }
13 ?>

This script creates an array, $a, on line 02. Three members are then added to the array: 'apples', 'oranges' and 'bananas'. To do this, the script 'pushes' new members on to the array $a. The term 'push' is used by PHP, since we are treating the array as a dynamic data structure, onto which we can stack more and more data. Notice on line 04 that two values are added by a single array_push() call. PHP interprets any arguments after the first to be new members for the specified array.

At line 05 , the script iterates through $a using the each() routine. The first call to each() returns an array consisting of the first member of $a and its key. In doing so, it increments an internal pointer, so that the next call returns the second member of $a, and so on. When all members have been returned, each() returns false, breaking the loop.

In conjunction with each(), the list() function is used. This is something of a quirk of the PHP syntax, but basically it allows developers to populate variables from an array returned by each() (or any other array returning function).

Line 06 outputs the data returned by each() to the user.

Line 08 resets the internal point of the array to the first item while line 09 'pops' the most recently pushed item off $a - this item is no longer a member of $a. The script then loops through $a again, proving that the popped item is no longer a member of the array.

Page Break

A closer look at arrays: Part II

The prvious page defined what arrays are, how they can be used in PHP and the elementary routines available in PHP to manipulate them. This time we will look at more advanced usage, including a number of support functions that provide sophisticated array usage, as well as methods of sorting arrays.

Advanced array usage

PHP has a large range of useful functions which can manipulate arrays. Some of the most widely-used ones are presented here. array_merge() can be used to join two or more arrays together. For example:

01 <?
02 $a = array(1,2);
03 $b = array(3,4);
04 $c = array_merge($a,$b);
05 ?>

Array $c on line 04 consists of the members of $a and $b, that is, 1, 2, 3 and 4. Any number of arguments can be passed to array_merge(). array_unique() is used to remove duplicate values from an array. Consider the following:

01 <?
02 $a = array(1,1,2,2,3);
03 $b = array_unique($a);
04 ?>

The array $b created on line 03 is a unique set of members from $a, i.e., 1, 2 and 3. It is often the case that a PHP script needs to check if a value is in an array. A developer could iterate through the array, checking each value, but the following is much simpler:

01 <?
02 $a = array(1,2,3,4);
03 if(in_array(1,$a)) {
04 echo "Value is in array\n";
05 }
06 ?>

The in_array() function tests whether the value passed as the first argument is in the array passed as the second argument. It is also useful to be able to retrieve a random value from an array. This can be done with the array_rand() routine.

01 <?
02 $a = array(1,2,3,4,5);
03 srand((double) microtime() * 10000000);
04 $key = array_rand($a,1);
05 echo "Random value = {$a[$key]}\n";
06 ?>

On line 03, the script seeds the random number generator. This is important in randomising the number used by array_rand(). The seed the script generates is based on the number of microns since the last whole second. For more discussion on this point, see: www.php.net/manual/en/function.srand.php.

On line 04, array_rand() returns a random key from $a. The second argument to array_rand() tells the routine to return only one random key. On line 05, the script retrieves a random value using the random key. The function shuffle() can be used to randomise the whole array.

Sorting an array

It is often important to sort arrays. Sorting a basic single dimensional array is simple.

01 <?
02 $a = range(1,20);
03 srand((double)microtime() * 1000000);
04 shuffle($a);
05 sort($a);
06 ?>

On line 02, the script defines an array whose members range from 1 to 20 - that is, 1, 2, 3, etc. The script randomises the array on lines 03 and 04. The array is then resorted on line 05.

Things get more complicated when you consider multi-dimensional arrays, but PHP has several functions to help developers with this. Consider the following:

01 <?
02 function cmp($a,$b)
03 {
04 $at = strtotime($a["date"]);
05 $bt = strtotime($b["date"]);
06 if($at == $bt)
07 return(0);
08 if($at > $bt)
09 return(1);
10 else
11 return(-1);
12 }
13
14 $a = array(array("title"=>"Title 1","date"=>" June 11 2003"),
15 array("title"=>"Title 2","date"=>"May 30 2003"),
16 array("title"=>"Title 3","date"=>"July 1 2003"));
17
18 usort($a,"cmp");
19 ?>

The script sorts the array $a according to the date field in each secondary array. To do this, the script declares a user-defined comparison function on line 02. This function receives two arguments, $a and $b. Each is an array from $a defined on line 14. In order to simplify sorting, the script defines $at and $bt, the UNIX timestamp representation of the dates defined in $a and $b, respectively. If these values are equal, cmp() returns 0. If $at is greater than $bt, the script returns 1; otherwise, -1.

With this defined, it is as simple as passing the multi-dimensional array the name of the comparison function to usort() on line 18 - PHP takes care of the rest.

There are many more array functions and many other ways to use arrays in PHP. For more information and examples, be sure to visit www.php.net/manual/en/ref.array.php.