The two ways of iterating through arrays

void list ( mixed ...)

array each ( array input)

If you do not specify a key, as in the first example, PHP will just assign incrementing numbers starting with 0. However, these numbers cannot be guaranteed to exist within the array in any given order, or even to exist at all - they are just key values themselves. For example, an array may have keys 0, 1, 2, 5, 3, 6, 7. That is, it can have its keys out of order or entirely missing. As a result, code like this should generally be avoided:

<?php
    for ($i = 0; $i < count($array); ++$i) {
        print $array[$i];
    }
?>

However, there is a quick and easy way to accomplish the same thing: a foreach loop, which itself has two versions. The easiest way to use foreach looks like this:

foreach($array as $val) {
    print $val;
}

Here the array $array is looped through and its values are extracted into $val. In this situation, the array keys are ignored completely, which usually makes most sense when they have been auto-generated (i.e. 0, 1, 2, 3, etc).

The second way to use foreach does allow you to extract keys, and looks like this:

foreach ($array as $key => $val) {
    print "$key = $val\n";
}

Another commonly used way to loop over arrays is using the list() and each() functions, like this:

<?php
    while (list($var, $val) = each($array)) {
        print "$var is $val\n";
    }
?>

List() is a function that does the opposite of array() - it takes an array, and converts it into individual variables. Each() takes an array as its parameter, and returns the current key and value in that array before advancing the array cursor. "Array cursor" is the technical term for the element of an array that is currently being read. All arrays have a cursor, and you can freely move it around - it is used in the while loop above, where we need to iterate through an array. To start with, each() will return the first element, then the second element, then the third, and so on, until it finds there are no elements left, in which case it will return false and end the loop.

The meaning of that first line is "get the current element in the array, and assign its key to $var and its value to $val, then advance the array cursor. There is a lot more detail on array cursors later.

Generally speaking, using foreach loops is the most optimised way to loop through an array, and is also the easiest to read. In practice, however, you will find foreach loops and list()/each() loops in about equal proportions, despite the latter option being slower. The key difference between the two is that foreach automatically starts at the front of the array, whereas list()/each() does not.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: The array operator >>

Previous chapter: Associative arrays

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.