note 104587 modified in class.recursiveiteratoriterator by danbrown

From: Date: Fri, 24 Jun 2011 15:29:40 +0000
Subject: note 104587 modified in class.recursiveiteratoriterator by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180923@lists.php.net to get a copy of this message
A very important thing to note about \RecursiveIteratorIterator is that it returns a flattened array when used with the iterator_to_array function. Ex: <?php $arr = array('Zero', 'name'=>'Adil', 'address' => array( 'city'=>'Dubai', 'tel' => array('int' => 971, 'tel'=>12345487)), '' => 'nothing'); $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr)); var_dump(iterator_to_array($iterator,true)); ?> This code will return : array(6) { [0]=> string(4) "Zero" ["name"]=> string(4) "Adil" ["city"]=> string(5) "Dubai" ["int"]=> int(91) ["tel"]=> int(12345487) [""]=> string(7) "nothing" } To get the non-flattened proper array use the getArrayCopy() method, like so : $iterator->getArrayCopy() This will return array(4) { [0]=> string(4) "Zero" ["name"]=> string(4) "Adil" ["address"]=> array(2) { ["city"]=> string(5) "Dubai" ["tel"]=> array(2) { ["int"]=> int(91) ["tel"]=> int(12345487) } } [""]=> string(7) "nothing" } --was-- A very important thing to note about \RecursiveIteratorIterator is that it returns a flattened array when used with the iterator_to_array function. Ex: //[CODE START] $arr = array('Zero', 'name'=>'Adil', 'address' => array( 'city'=>'Dubai', 'tel' => array('int' => 971, 'tel'=>12345487)), '' => 'nothing'); $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr)); var_dump(iterator_to_array($iterator,true)); //[CODE END] This code will return : array(6) { [0]=> string(4) "Zero" ["name"]=> string(4) "Adil" ["city"]=> string(5) "Dubai" ["int"]=> int(91) ["tel"]=> int(12345487) [""]=> string(7) "nothing" } To get the non-flattened proper array use the getArrayCopy() method, like so : $iterator->getArrayCopy() This will return array(4) { [0]=> string(4) "Zero" ["name"]=> string(4) "Adil" ["address"]=> array(2) { ["city"]=> string(5) "Dubai" ["tel"]=> array(2) { ["int"]=> int(91) ["tel"]=> int(12345487) } } [""]=> string(7) "nothing" } http://php.net/manual/en/class.recursiveiteratoriterator.php

« previous php.notes (#180923) next »