3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'children' => [ [ 'id' => 123, 'title' => 'test', 'children' => [ [ 'id' => 345, 'title' => 'test 1', 'children' => [], ], [ 'id' => 567, 'title' => 'test 2', 'children' => [ [ 'id' => 789, 'title' => 'test 3', 'children' => [], ], [ 'id' => 333, 'title' => 'tset 4', 'children' => [ [ 'id' => 222, 'title' => 'test 5', 'children' => [], ], [ 'id' => 111, 'title' => 'test 55', 'children' => [], ], [ 'id' => 444, 'title' => 'test 556', 'children' => [], ], [ 'id' => 666, 'title' => 'test44', 'children' => [], ], ], ], [ 'id' => '5556', 'identifier' => 'test 2', 'children' => [ [ 'id' => 888, 'title' => 'test 2', 'children' => [], ], [ 'id' => 255, 'title' => 'tset', 'children' => [], ], [ 'id' => 454, 'title' => 'warm-up-5837', 'children' => [], ], ], ], ], ], ], ], ], ]; function flattenTree(array $tree): array { $flat = []; foreach ($tree['children'] ?? [] as $child) { $flat[] = array_filter($child, 'is_scalar'); if (!empty($child['children'])) { array_push($flat, ...flattenTree($child, $flat)); } } return $flat; } var_export(flattenTree($array));
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
array ( 0 => array ( 'id' => 123, 'title' => 'test', ), 1 => array ( 'id' => 345, 'title' => 'test 1', ), 2 => array ( 'id' => 567, 'title' => 'test 2', ), 3 => array ( 'id' => 789, 'title' => 'test 3', ), 4 => array ( 'id' => 333, 'title' => 'tset 4', ), 5 => array ( 'id' => 222, 'title' => 'test 5', ), 6 => array ( 'id' => 111, 'title' => 'test 55', ), 7 => array ( 'id' => 444, 'title' => 'test 556', ), 8 => array ( 'id' => 666, 'title' => 'test44', ), 9 => array ( 'id' => '5556', 'identifier' => 'test 2', ), 10 => array ( 'id' => 888, 'title' => 'test 2', ), 11 => array ( 'id' => 255, 'title' => 'tset', ), 12 => array ( 'id' => 454, 'title' => 'warm-up-5837', ), )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
115.59 ms | 408 KiB | 5 Q