3v4l.org

run code in 300+ PHP versions simultaneously
<?php $example = [ ['text' => 'a', 'children' => [ ['text' => 'b'], ['text' => 'c'], ['text' => 'd', 'children' => [ ['text' => 'e'], ['text' => 'f'], ['text' => 'g', 'children' => [ ['text' => 'h'], ['text' => 'i'], ['text' => 'j'], ]], ['text' => 'k'], ['text' => 'l'], ['text' => 'm'], ]], ['text' => 'n'], ['text' => 'o'], ['text' => 'p'], ]] ]; function listElements(array $input, $depth = 0) { end($input); $final = key($input); $n = count($input); foreach ($input as $key => $element) { echo $element['text']; echo ($key == $final) ? " last of $n," : ''; echo " depth $depth", PHP_EOL; if (isset($element['children'])) { listElements($element['children'], $depth + 1); } } } listElements($example);

preferences:
27.33 ms | 405 KiB | 5 Q