ArrayIterator class

Earlier we implemented our own array iterator MyArrayIterator. The good new is, PHP provides an array iterator, so we don’t need to implement our own.

class  ArrayIterator implements ArrayAccess , SeekableIterator , Countable , Serializable {

    /* Methods */
    public __construct ([ mixed $array = array() [, int $flags = 0 ]] )

    // Countable
    public int count ( void )

    // Seekable extends Iteartor
    public mixed current ( void )
    public mixed key ( void )
    public void rewind ( void )
    public void next ( void )
    public bool valid ( void )
    public void seek ( int $position )

    // ArrayAccess methods
    public void offsetExists ( string $index )
    public mixed offsetGet ( string $index )
    public void offsetSet ( string $index , string $newval )
    public void offsetUnset ( string $index )

    // Serialize
    public string serialize ( void )
    public string unserialize ( string $serialized )

    // unique class methods...
    // flag options
    /*
      0 = Properties of the object have their normal functionality when accessed as list (var_dump, foreach, etc.).
      1 = Array indices can be accessed as properties in read/write.
    */
    public void setFlags ( string $flags )
    public void getFlags ( void )
    public array getArrayCopy ( void )

    public void append ( mixed $value )
    // sorting methods
    public void asort ( void )
    public void uasort ( string $cmp_function )
    public void uksort ( string $cmp_function )
    public void ksort ( void )
    public void natcasesort ( void )
    public void natsort ( void )
}

This iterator allows you to both unset and modify values and keys while iterating over arrays and objects. ArrayIterator can be created by passing an array to the constructor. If you have an ArrayObject, calling ArrayObject::getIterator() returns an ArrayIterator. ArrayIterator instances are created implicitly when ``foreach`` encounters an ArrayObject. Example:

<?php
$fruits = array(
                "apple" => "yummy",
                "orange" => "ah ya, nice",
                "grape" => "wow, I love it!",
                 "plum" => "nah, not me"
                );

$grocery = array($fruits);

foreach (new ArrayIterator($fruits) as $key => $value) {

       echo $key . ' => ' . $value ."\n";
       $fruits[$key] .= " altered";
}

foreach (new ArrayIterator($fruits) as $key => $value) {

       echo $key . ' => ' . $value ."\n";
       $fruits[$key] .= " altered";
}

ArrayIterator wraps these PHP functions for built–in PHP arrays: ksort, natcasesort, natsort, uasort, and uksort.

ArrayIterator implements ArrayAccess , SeekableIterator , Countable , Serializable {//...}
    //. . . snip
    public function asort ( void );
    public function ksort ( void );
    public function natcasesort ( void );
    public function natsort ( void );
    public function uasort ( string $cmp_function );
    public function uksort ( string $cmp_function );
}

ArrayIterator can only iterate one dimensional arrays. If an array contains subarrays, these are not returned. In order to iterator over a multidimenstional array you must create an instance of RecursiverArrayIterator.