Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays Wish List | Report Bug | About Me ]

5.8     The array cursor: reset(), end(), next(), and prev()

This is NOT the latest copy of this book; click here for the latest version.

mixed reset ( array array)

mixed end ( array array)

mixed next ( array array)

mixed prev ( array array)

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.





<< 5.7 Multidimensional arrays   5.9 Holes in arrays >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow