RecursiveArrayIterator class

RecursiveArrayIterator extends ArrayIterator and implements RecursiveIterator, implementing the two RecursiveIterator interface methods of getChildren() and hasChildren().

This iterator allows you to unset and modify values and keys while iterating over Arrays and Objects in the same way as ArrayIterator, and, in addition, you can iterate over the current iterator entry. The use of ArrayIterator is straight forward, but limited to single dimensional arrays. Sometimes you’ll have a multidimensional array and you’ll want to iterate through the nested arrays recursively. This is when to use RecursiveArrayIterator. Like ArrayIterator, RecursiveArrayIterator also contains the append method and the same sorting methods.

RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {

    public ArrayIterator::__construct ([ mixed $array = array(); [, int $flags = 0 ]] );
    public array ArrayIterator::getArrayCopy ( void );

    /* RecursiveIterator methods */

    public RecursiveArrayIterator getChildren ( void );
    public bool hasChildren ( void );

    /* Inherited methtods below, categorize by interface */

    // Seekable interface (which extends Iterator) methods
    public void ArrayIterator::seek ( int $position );
    public mixed ArrayIterator::key ( void );
    public void ArrayIterator::next ( void );
    public bool ArrayIterator::valid ( void );
    public mixed ArrayIterator::current ( void );
    public void ArrayIterator::rewind ( void );

    // Countable methods
    public int ArrayIterator::count ( void );

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

    // Serializable interface methods
    public string ArrayIterator::serialize ( void );
    public string ArrayIterator::unserialize ( string $serialized );

    // other class methods
    public void ArrayIterator::append ( mixed $value );
    public void ArrayIterator::asort ( void );
    public void ArrayIterator::setFlags ( string $flags );
    public void ArrayIterator::getFlags ( void );
    public void ArrayIterator::uasort ( string $cmp_function );
    public void ArrayIterator::uksort ( string $cmp_function );
    public void ArrayIterator::ksort ( void );
    public void ArrayIterator::natcasesort ( void );
    public void ArrayIterator::natsort ( void );
}

To do recursive iteration, use pass RecurisveArrayIterator to RecursiverIteratorIterator.

<?php

 // An array containing four elements, the first three are nested subarrays.
 $arr = [
     ["sitepoint", "phpmaster"],      // first nested array
     ["buildmobile", "rubysource"],   // second nested array
     ["designfestival", "cloudspring"], // third nested array
     "not a subarray"                     // one-dimensional array of one element
 ];

 $riteriter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)); // RecursiveIteratorIterator::CHILD_FIRST);

 foreach($riteriter as $key => $value ) {

     echo "$key => $value \n";

 }

This will output:

0 => string(9) "sitepoint"
1 => string(9) "phpmaster"
0 => string(11) "buildmobile"
1 => string(10) "rubysource"
0 => string(14) "designfestival"
1 => string(11) "cloudspring"
3 => string(12) "not a subarray"

Changing the iterator mode to RecursiveIteratorIterator::CHILD_FIRST:

<?php
 $riteriter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)); // RecursiveIteratorIterator::CHILD_FIRST);

 foreach($riteriter as $key => $value ) {

     echo "$key => $value \n";

 }

will give this output:

0 => string(9) "sitepoint"
1 => string(9) "phpmaster"
0 => array(2) {
  [0] =>
  string(9) "sitepoint"
  [1] =>
  string(9) "phpmaster"
}
0 => string(11) "buildmobile"
1 => string(10) "rubysource"
1 => array(2) {
  [0] =>
  string(11) "buildmobile"
  [1] =>
  string(10) "rubysource"
}
0 => string(14) "designfestival"
1 => string(11) "cloudspring"
2 => array(2) {
  [0] =>
  string(14) "designfestival"
  [1] =>
  string(11) "cloudspring"
}
3 => string(12) "not an array"