note 115192 added to class.recursiveiteratoriterator
| From: | gerry at king-foo dot be | Date: | Wed, 11 Jun 2014 12:44:05 +0000 |
| Subject: | note 115192 added to class.recursiveiteratoriterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199650@lists.php.net to get a copy of this message | ||
Carefull when using iterator_to_array(). Because it flattens down your subiterators, elements with
the same keys will overwrite eachother.
For example:
<?php
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator([
['foo', 'bar'],
['baz', 'qux']
])
);
foreach ($iterator as $element) {
echo $element;
}
?>
This will output all 4 elements as expected:
string(3) "foo"
string(3) "bar"
string(3) "baz"
string(3) "qux"
While doing:
<?php
var_dump(iterator_to_array($iterator));
?>
will output an array with only the last 2 elements:
array(2) {
[0]=>
string(3) "baz"
[1]=>
string(3) "qux"
}
----
Server IP: 72.52.91.14
Probable Submitter: 212.88.227.250
----
Manual Page -- http://php.net/manual/en/class.recursiveiteratoriterator.php
Edit -- https://master.php.net/note/edit/115192
Del: integrated -- https://master.php.net/note/delete/115192/integrated
Del: useless -- https://master.php.net/note/delete/115192/useless
Del: bad code -- https://master.php.net/note/delete/115192/bad+code
Del: spam -- https://master.php.net/note/delete/115192/spam
Del: non-english -- https://master.php.net/note/delete/115192/non-english
Del: in docs -- https://master.php.net/note/delete/115192/in+docs
Del: other reasons-- https://master.php.net/note/delete/115192
Reject -- https://master.php.net/note/reject/115192
Search -- https://master.php.net/manage/user-notes.php