3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( array( 'id' => 1, 'parent_id' => 0, 'title' => 'Parent Page', 'children' => array( array( 'id' => 2, 'parent_id' => 1, 'title' => 'Sub Page', ), array( 'id' => 5, 'parent_id' => 1, 'title' => 'Sub Page 2', 'children' => array( array( 'id' => 7, 'parent_id' => 5, 'title' => 'Sub Page', ), array( 'id' => 8, 'parent_id' => 5, 'title' => 'Sub Page 2', ) ) ) ) ), array( 'id' => 4, 'parent_id' => 0, 'title' => 'Another Parent Page', ) ); function printAll($arr, $parent = []) { if (is_array($arr)) { foreach ($arr as $k => $v) { if (isset($v['id'])) { $parent[] = $v['id']; echo implode('.', $parent) . PHP_EOL; } if (isset($v['children'])) { printAll($v['children'], $parent); } array_pop($parent); } } } printAll($arr);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
1 1.2 1.5 1.5.7 1.5.8 4

preferences:
77.87 ms | 407 KiB | 5 Q