3v4l.org

run code in 300+ PHP versions simultaneously
<?php $myArray = array( 0, array( 0, 1, array( 0, 1, 2, array( 0, 1, 2, 3, array("the end") ) ) ) ); $recursive_array_iterator = new RecursiveArrayIterator($myArray); iterator_apply($recursive_array_iterator, 'traverseRecursiveArray', array($recursive_array_iterator)); $i = 0; function traverseRecursiveArray($iterator) { global $i; while ($iterator->valid()) { if ($iterator->hasChildren()) { traverseRecursiveArray($iterator->getChildren()); } else { echo $iterator->key() . ' => ' . $iterator->current() . PHP_EOL; } $iterator->current = "d"; $iterator->next(); } } iterator_apply($recursive_array_iterator, 'traverseRecursiveArray', array($recursive_array_iterator));

preferences:
40.6 ms | 402 KiB | 5 Q