Day 5 – Day for Arrays

Zend PHP Certification

Let me start off by saying, "Merry Xmas!"  It’s been a long day of family, friends, fun, and traveling, but I have time now to sit down and study/post.  Today is still dealing with arrays, and there’s a lot to cover.


Iterating Arrays

The most obvious choice is to iterate via a for loop like I said yesterday, but what happens when your keys are not numeric or in order?  You can use one of two options:

  • Array Pointers are designed to start at one side of an array and "walk" through either forwards or backwards.  Please note that when using these functions, you are not navigating the array in order of the keys or values, but in the order they were added/stored.  The following functions are available to perform this procedure:
    • reset($array): moves the pointer to the front of the array.
    • end($array): moves the pointer to the end of the array.
    • current($array): returns the current value of the array at the pointer.
    • key($array): returns the current key of the array at the pointer.
    • next($array): moves the pointer forward in the array.
    • prev($array): moves the pointer backward in the array.

    All of the functions, with the exception of key() will return the value at the new index of the array.  They will also return false if the array has ended or is empty.  Now because you can store boolean false as a value, it’s important not to rely on current()’s return value to determine the end of the arrayKey() will return null if and only if the pointer has reached the end of the array.

    $array = array(1, 2, 3);
    1.  
    2. while (key($array) !== null) {
    3.   echo key($array)." => ".current($array);
    4.   next($array);
    5. }
  • Each / ForEach can allow you to walk through the loop in key/value pairs.  This only works in one direction, forward.

    $array = array(1, 2, 3);
    1.  
    2. while (list($key, $value) = each($array))
    3.   echo $key." => ".$value

    With each(), it moves the internal pointer forward on each call to that function.  So if you wanted to repeat this operation, you have to call reset().

    $array = array(1, 2, 3);
    1.  
    2. foreach ($array as $key => $value)
    3.   echo $key." => ".$value;

    When using foreach, the entire array is accessed from front to back, ignoring the internal pointer’s previous position.  If you wanted to modify these values, you could include an ampersand (&) before the $value variable.

  • Passive Iteration involves walking a user created function through each element in the array.  There are two functions, array_walk() and array_walk_recursive(), used for this operation.  The later is obviously used for multi-dimensional arrays (arrays within arrays).  Here is a basic example with a normal walk:

    function squared(&$value, &$key) {
    1.   $value *= $value;
    2. }
    3.  
    4. $array = array(1, 2, 3);
    5. array_walk($array, 'squared');
    6. // Array is now equal to (1, 4, 9)

Wow, I didn’t think there would be so much on simple array iteration.  Well, there is still more in this chapter, so tomorrow will have to be Sorting and Stacks/Queues.

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.