The array cursor

mixed reset ( array array)

mixed end ( array array)

mixed next ( array array)

mixed prev ( array array)

If you're just learning PHP, I would recommend skipping this section entirely - using foreach loops is easier, faster and cleaner.

Earlier on I mentioned that each array has a "cursor", which you can think of as an arrow pointing to the next array element in line to be operated on. It is the array cursor that allows code like while (list($var, $val) = each($array)) to work - each moves forward the array cursor of its parameter each time it is called, until it eventually finds itself at the end of the array, and so returns false, ending the loop.

Now, consider this next piece of code:

<?php
    $array
= array("Foo", "Bar", "Baz");
    print
"First time...\n";

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

    print
"Second time...\n";

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

The chances are, that piece of code will not do what you expect, because it will not print out the contents of $array the second time around. The reason for the strange result is because each() does not move the array cursor back to the first element when you first call it - it just picks up from where the cursor was. As a result, after our first while loop, the cursor is right at the end of the array, so that when it comes to the second while loop, the each() function returns false immediately because it is pointing to the last item in the array.

It is in situations like this where you need to set the position of the array cursor forcibly, and there are four functions to help you out: reset(), end(), next(), and prev(). They all take just one parameter, the array to work with, and return a value from the array.

Reset() rewinds its array parameter's cursor to the first element, then returns the value of that element, whereas end() set the array cursor to the last element, and returns that value. Next() and prev() both move the cursor pointer forward or backward one element respectively, returning the value of the element now pointed to. If any of the four functions cannot return a value (if there are no elements in the array, or if the array cursor has gone past the last element), they will return false. As such, you can use them all in loops if you want.

For example, to iterate through an array in reverse, use this code:

<?php
    $array
= array("Foo", "Bar", "Baz", "Wom", "Bat");
    print
end($array);

    while(
$val = prev($array)) {
        print
$val;
    }
?>

Note that we print the output of end(), because it sets the array cursor to point at "Bat", and prev() will shift the array cursor back one to "Wom", meaning that "Bat" would otherwise not be printed out.

 

Next chapter: Holes in arrays >>

Previous chapter: Multidimensional arrays

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar