<?php
$myArray = array(
0 => "a",
1 => array(
0 => "a",
1 =>"b",
2 => array(
0 =>"a",
1 =>"b",
2 =>"c",
3 => new ArrayObject(
0 => "a",
1 => "b",
3 => "c",
4 => "d",
5 => array("the end")
)
)
)
);
$recursive_array_iterator = new RecursiveArrayIterator(new ArrayObject($myArray));
iterator_apply($recursive_array_iterator, 'traverseRecursiveArray', array($recursive_array_iterator));
var_dump($recursive_array_iterator);
function traverseRecursiveArray($iterator)
{
$i = 0;
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);
- Output for 5.4.0 - 5.4.31
- Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /in/RBLqY on line 13
Process exited with code 255. - Output for 5.3.0 - 5.3.28
- Parse error: syntax error, unexpected T_DOUBLE_ARROW in /in/RBLqY on line 13
Process exited with code 255.
preferences:
197.92 ms | 1395 KiB | 68 Q