Using Array Pointers

Along with the keys and the associated values stored in an array, PHP maintains an internal index that points to the current element in the array. Several functions use and update this array index to provide access to elements in the array. To illustrate how this internal index can be used, consider the following example:

$a = array("a", "b", "c", "d", "e", "f");
echo current($a  );     // prints "a"
// Array ( [1]=> a [value]=> a [0]=> 0 [key]=> 0 )
print_r each($a);
// Array ( [1]=> b [value]=> b [0]=> 1 [key]=> 1 )
print_r each($a);
// Array ( [1]=> c [value]=> c [0]=> 2 [key]=> 2 )
print_r each($a);
echo current($a  );     // prints "d"

The internal index is set to point at the first element when a new array is created, and the function current( ) returns the value pointed to by the array's internal index. The function each( ) returns an array that holds the index key and the value of the current element, and then increments the internal index of the array to point at the next element. The array each( ) returns has four elements: two that hold the key, accessed by the numeric index 0 and the associative key key; and two that hold the value, accessed by the numeric index 1 and the associative key value.

Other functions that use the array's internal pointer are end( ), next( ), prev( ), reset( ), and key( ).

Before the foreach statement was introduced to the PHP language, a common way to iterate through an associative array was to use a while loop with the each( ) function to get the key/value pairs for each element and the list( ) function to assign these values to variables. The following example shows how such an iteration is performed through the $sounds array to generate verses of "Old MacDonald":

$sounds = array ("pig"=>"oink", "cow"=>"moo",
                 "duck"=>"quack", "dog"=>"woof");
while (list($animal, $sound) = each($sounds))
  echo "<p>Old MacDonald had a farm EIEIO";
  echo "<br>And on that farm he had a $animal EIEIO";
  echo "<br>With a $sound-$sound here";
  echo "<br>And a $sound-$sound there";
  echo "<br>Here a $sound, there a $sound";
  echo "<br>Everywhere a $sound-$sound";

The foreach statement is clearer and should be used in most cases. However we include the while loop example here because many existing scripts use this structure to iterate through an associative array.

The list( ) function isn't really a function, but a language construct that assigns multiple variables from an array expression:

list($var1, $var2, ...) = array_expression

list( ) appears on the left side of an assignment and an array expression appears on the right. The arguments to list( ) must be variables. The first variable is assigned the value of the first element in the array, the second variable the value from the second element, and so on. We avoid using the list( ) construct, because its use leads to assumptions about the number of elements in an array. The need to use list( ) to access the key/value pairs returned from each( ) is avoided with the foreach statement.