One of the most useful features of arrays is that it is possible to loop through them and repeatedly perform processes on the individual elements.
Sequentially Indexed Arrays
The simplest way to loop through array is to use count() to determine the number of elements in an array, and then construct a for() loop:
$countries = array ("cr", "de", "us");
$num_elements = count ($countries);
// $num_elements now has value of 3
for ($idx = 0; $idx < $num_elements; ++$idx) {
// Print each element on its own line:
echo ("$countries[$idx] <BR>\n");
}
Non-Sequentially Indexed Arrays
An array has a built in pointer (or iterator). This internal cursor keeps track of which element currently is in focus. For a newly created array, the pointer is on the first element. We can determine the value of the current element using the current() function, and the current element's index using the key() function. To illustrate these, the following code initializes an array and prints the value and index of the current element:
$countries[50] = "cr";
$countries[20] = "de";
$countries[10] = "us";
$countries[] = "uk";
// Has index '51'
$key = key ($countries);
$value = current ($countries);
echo ("Element $key equals $value");
Because the array has just been created, current element is the first, so this code will print Element 50 equals cr. Note that "cr" is the first element in this array, even though "de" and "us" have lower indices, because it was the first element to be assigned to the array. Arrays are not ordered unless we specifically call a function to sort the array; we will see how to do this later.
The two functions each() and list() can be used together to loop through an array, even if the indices are non sequential (or even if they are not numbers at all; non-numeric keys will be covered in the section, 'String Indexed Arrays'). An example of a loop that successfully navigates an array whether or not it is sequentially indexed is demonstrated below:
reset ($countries);
while (list ($key, $value) = each ($countries)) {
echo "Element $key equals $value<BR>\n";
}
In the section on 'String-Indexed Arrays', we'll examine more closely how this works. For now, think of the line:
while (list ($key, $value) = each ($countries)) {
to mean "for each element in the array, set $key equal to the element's key (or index), and $value equal to the value of the element." (Some languages handle this with a for each construct.) The reset() function sets the internal pointer to the first element. Naturally, this isn't needed if the pointer is already on the first element; but it can be a useful habit to include reset() before traversing the array, just to make sure we start at the beginning. The each() function moves the array pointer one element forward every time it is called, so if our loop were nested, reset() would be needed to restore the pointer to the first element (assuming we want to start at the beginning, of course). Using our previously assigned $countries array, the output of the above code would be:
Element 50 equals cr
Element 20 equals de
Element 10 equals us
Element 51 equals uk