Looping through Non-Sequential Arrays

This is a perfect time to use a while loop. Ofcourse, you will be using the while loop in conjunction with three other functions: reset(), list(), and each(). The reset function sets the current index of the array back to the first element available. It takes one argument, and that is the array that you would like to reset.

reset($someArray);

The list function takes two parameters: a variable to store the index of the element and a variable to store the value of the element.

list($index, $value);

The function each() takes one argument, and that is the array you wish to iterate on.

each($someArray);

Take a look at the following example of how to use these three functions together in a while loop to iterate through a non-sequential array.

<?php
$board = array(“a”, 7 => “b”, 25 => “c”, 50 => “d”);
reset($board);
while(list($index, $value) = each($board))
{
    echo(“Index = “ . $index . ”, Element = “ . $value . ”<BR>”);
}
?>

The results of the example above appear exactly like Figure 6.3, even though the indexes in this particular array are not in a sequential order. Calling the reset() function in this example is quite unnecessary because, since the array has just been created, the current index is obviously the first available element. But it is good practice to get into, because you might end up trying to loop through an array and it could be starting in the middle of it, and that would be no good. PHP also offers several other functions to operate on arrays, such as current(), key(), next(), and prev() to name just a few. For a complete list of functions that PHP supports, please refer to Appendix B. Take a look at the following example and Figure 6.4 to see what the key() and current() functions do:

Figure 6.4. An example of the key() and current() functions.


<?php
$board = array(“a”, “b”, “c”, “d”);
$index = key($board);
$value = current($board);
echo(“Index = “ . $index . ”, Element = “ . $value . ”<BR>”);
?>

NOTE

Key and index mean exactly the same thing; the function in PHP is called key() instead of index().

Earlier I mentioned next() and prev(). These two functions are another way to navigate through an array. As you might imagine, next() goes to the next available index in the array, and returns the element. prev() does the same thing but instead of going to the next available index, it goes to the previous index. Take a look at the following loop:

<?php
$board = array(“a”, “b”, “c”, “d”);
reset($board);
for($i = 0; $i < 4; $i++)
{
    $index = key($board);
        $value = current($board);
    next($board);
        echo(“Index = “ . $index . ”, Element = “ . $value . ”<BR>”);
}

This will print the same results to the browser as Figure 6.3 did, but it does so in a different way. But be careful if you use the next() and prev() functions. Remember I said it returns the value of the element of the next index in the array? Well, what if the value is 0? Take a look at this example:

<?php
$board = array(3, 0, 2, 4);
do
{
    $index = key($board);
    $value = current($board);
    echo(“Index = “ . $index . ”, Element = “ . $value . ”<BR>”);
} while(next($board));
?>

This loop will stop when it gets to the second element of the array because the while statement will evaluate to false. So be very careful if you use the next() and prev() functions. If you ever run into a loop that is stopping abruptly, take a look to make sure that your Boolean expression isn’t being evaluated to false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset