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"
}