3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tree = [ [ "id" => 1573695284631, "name" => "Cars", "pid" => 0, "children" => [ [ "id" => 1573695292010, "name" => "Audi", "pid" => 1573695284631 ], [ "id" => 1573695305619, "name" => "BMW", "pid" => 1573695284631, "children" => [ [ "id" => 1573695328137, "name" => "3 Series", "pid" => 1573695305619 ], [ "id" => 1573695335102, "name" => "X5", "pid" => 1573695305619 ] ] ] ] ], [ "id" => 1573695348647, "name" => "Motorcycles", "pid" => 0, "children" => [ [ "id" => 1573695355619, "name" => "Ducatti", "pid" => 1573695348647 ] ] ] ]; var_dump(getParentNodes($tree, 1573695335102)); function getParentNodes($haystack, $child_node_id) { foreach ($haystack as $element) { if ($element['id'] === $child_node_id) { // return [$element['id']]; // uncomment if you want to include child itself return []; } else if (array_key_exists('children', $element)) { $parentNodes = getParentNodes($element['children'], $child_node_id); if ($parentNodes !== false) { return [$element['id'], ...$parentNodes]; } } } return false; }
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
array(2) { [0]=> int(1573695284631) [1]=> int(1573695305619) }
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.
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(2) { [0]=> int(1573695284631) [1]=> int(1573695305619) }
Output for 7.3.0 - 7.3.33
Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' in /in/umhv9 on line 58
Process exited with code 255.

preferences:
175.33 ms | 410 KiB | 5 Q