Tips : RecursiveArrayIterator on mulitdimensional Array

When we have a multidimensional array we have to make some recursives function to parse it. A simple way to get the keys and the value of this type of array is to use the SPL library of PHP.

To do this we will use a RecursiveArrayIterator and a RecursiveIteratorIterator

$array_multi = array( 
      "myKey" => "myValue",
      "myKey2"=> array(
                   "myKey2Array" => "value2Array",
                   "myKey3Array" => "value3Array",
                   "myKey4Array" => "value4Array",
                   "myKey5Array" => array("test", "tata", "france")
                     )
);
 
$array_iterator = new RecursiveIteratorIterator(
                     new RecursiveArrayIterator($array_multi)
                  );
 
foreach($array_iterator as $key=>$value){
     echo $key.' -- '.$value.'
';
}

This simple code will just output all the keys/value of the array :

myKey — myValue
myKey2Array — value2Array
myKey3Array — value3Array
myKey4Array — value4Array
0 — test
1 — tata
2 — france


About this entry