<?php
$myArray = array(
"a",
array(
"a",
"b",
array(
"a",
"b",
"c",
array(
"a",
"b",
"c",
"d",
array("the end")
)
)
)
);
$recursive_array_iterator = new RecursiveArrayIterator($myArray);
iterator_apply($recursive_array_iterator, 'traverseRecursiveArray', array($recursive_array_iterator));
function traverseRecursiveArray($iterator)
{
while ($iterator->valid()) {
if ($iterator->hasChildren()) {
traverseRecursiveArray($iterator->getChildren());
} else {
$iterator->offsetSet($iterator->key(), manipulate($iterator->current()));
echo $iterator->key() . ' => ' . $iterator->current() . PHP_EOL;
}
$iterator->next();
}
}
function manipulate($string)
{
$string .= " appended";
return $string;
}
var_dump($myArray);
preferences:
24.98 ms | 405 KiB | 5 Q