PHP: Stepping Through Arrays

I’ve posted a few articles about working with arrays in PHP over the past few years. I have posted information about adding and replacing elements in arrays; searching for items in arrays; and even a general post about handy array-related functions in PHP. I’m back again with a few more handy functions.

In PHP, you can obviously loop through arrays pretty easily by using a foreach() loop, but did you know you can actually step through arrays manually? PHP offers a handful of functions to do just that. Let’s take a look at those functions, and how you might be able to use them.

Reset

To start with, you have the reset() function. This function resets the internal pointer of the array to the very beginning. As a bonus, it actually returns the value of the first element in the array, too. This can be extremely handy for times when you need to figure out what the first value in an array is without being destructive. Where array_shift() acts byRef on an array; meaning that it actually alters the input array when it returns the first value; the reset() function will not alter the input array. Therefore, instead of using code that might look like the following to find the first value in the array, you can use the code shown in the second block.

<?php
$myArr = array( 'first value', 'second value', 'third value' );
$myCopy = $myArr;
$myFirstVal = array_shift( $myCopy );
?>
<?php
$myArr = array( 'first value', 'second value', 'third value' );
$myFirstVal = reset( $myCopy );
?>

End

Another function that can come in handy is the end() function. This function sets the internal pointer to the last element in the array, and returns its value. Just as reset() is non-destructive, so is end(). Therefore, it can make a handy alternative to the array_pop() function (which, as with array_shift(), alters the input array).

Current

The current() function will leave the internal pointer exactly where it is, and return the value of the current array element.

Next and Previous

The next() and previous() functions will advance the internal pointer by one element and rewind the internal pointer by one element (respectively) and return the value of the element on which the pointer ends up. If you try to use next() when the pointer is already at the last element, or you try to use prev() when the pointer is already at the first element, the function will return boolean false. Therefore, you could test to see if you’re at the end of the array by using something like:

<?php
if( false === next( $myArr ) ) {
    echo 'You are at the end of the array already';
}
?>

More Functions

The functions listed above are the standard functions for manually stepping through arrays, but there are two others that may help you along the way. The each() function will return an array containing the key and the value of the current element in the array. The key() function will return just the key of the current element.

Putting it Together

Let’s say you want to actually manually step through an array. You could use some code similar to:

<?php
$myArr = array( 'First value', 'Second value', 'Third value', 'Fourth value' );
reset( $myArr ); /* Make sure we're at the start */
/* Loop forwards through the array */
while( false !== next( $myArr ) ) {
    print( current( $myArr ) );
}
end( $myArr ); /* Make sure we're at the end */
/* Loop backwards through the array */
while( false !== prev( $myArr ) ) {
    print( current( $myArr ) );
}
?>

Post Your Comment

Your email address will not be published.