3v4l.org

run code in 500+ PHP versions simultaneously
<?php $menu = [ 'root' => [ 'items' => [ 'A' => [ 'p' => [1, 2, 9], ], 'B' => [ 'p' => [1, 2, 3, 9], ], 'C' => [ 'p' => [1, 2, 4, 9], ], 'D' => [ 'items' => [ 'D1' => [ 'p' => [1, 2, 3, 4, 9], ], 'D2' => [ 'p' => [1, 2, 3, 4], ], ], ], 'E' => [ 'items' => [ 'E1' => [ 'p' => [1, 2, 10], ], ], ], 'F' => [ 'items' => [ 'F1' => [ 'p' => [5, 6], ], 'F2' => [ 'p' => [7, 8], ], ], ], ], ], ]; function recursive_ps($elem) { $output = []; if (isset($elem['items'])) { foreach($elem['items'] as $key => $value) { if (isset($value['p'])) { $output = array_merge($output, $value['p']); } if (isset($value['items'])) { // merge the return value of function $output = array_merge($output, recursive_ps($value)); } } } return $output; } $output = array_values(array_unique(recursive_ps($menu['root']))); print_r($output);
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.32, 8.3.0 - 8.3.4, 8.3.6 - 8.3.32, 8.4.1 - 8.4.23, 8.5.0 - 8.5.9
Array ( [0] => 1 [1] => 2 [2] => 9 [3] => 3 [4] => 4 [5] => 10 [6] => 5 [7] => 6 [8] => 7 [9] => 8 )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [0] => 1 [1] => 2 [2] => 9 [3] => 3 [4] => 4 [5] => 10 [6] => 5 [7] => 6 [8] => 7 [9] => 8 )

preferences:
129.23 ms | 1443 KiB | 4 Q