3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getTree($treeByParent, $treeByCat, $id, $lastId) { $gTree = []; if ($id != $lastId) { $gTree[] = $id; if ( isset($treeByCat[ $id ]) && count($treeByCat[ $id ]) > 0 ) $gTree = array_merge( getTree($treeByParent, $treeByCat, $treeByCat[ $id ], $id), $gTree); if ( isset($treeByParent[ $id ]) && count($treeByParent[ $id ]) > 0 ) { foreach ($treeByParent[ $id ] as $cat) { $gTree = array_merge( getTree($treeByParent, $treeByCat, $cat, $id), $gTree); } } return $gTree; } } $treeByParent = array( 0 => array(1, 2), 1 => array(3, 7), 3 => array(4), 4 => array(5), 5 => array(6, 8) ); $treeByCat = array( 1 => 0, 2 => 0, 3 => 1, 7 => 1, 4 => 3, 5 => 4, 6 => 5, 8 => 5 ); print_r( getTree($treeByParent, $treeByCat, 4) );
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught ArgumentCountError: Too few arguments to function getTree(), 3 passed in /in/KXDOM on line 49 and exactly 4 expected in /in/KXDOM:3 Stack trace: #0 /in/KXDOM(49): getTree(Array, Array, 4) #1 {main} thrown in /in/KXDOM on line 3
Process exited with code 255.
Output for 7.0.14 - 7.0.20
Warning: Missing argument 4 for getTree(), called in /in/KXDOM on line 49 and defined in /in/KXDOM on line 3 Notice: Undefined variable: lastId in /in/KXDOM on line 8 Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/KXDOM on line 11
Process exited with code 255.
Output for 7.0.0 - 7.0.6
Warning: Missing argument 4 for getTree(), called in /in/KXDOM on line 49 and defined in /in/KXDOM on line 3 Notice: Undefined variable: lastId in /in/KXDOM on line 8 Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/KXDOM on line 15
Process exited with code 255.
Output for 5.6.7 - 5.6.28
Warning: Missing argument 4 for getTree(), called in /in/KXDOM on line 49 and defined in /in/KXDOM on line 3 Notice: Undefined variable: lastId in /in/KXDOM on line 8 Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /in/KXDOM on line 9
Process exited with code 255.
Output for 5.5.24 - 5.5.35
Warning: Missing argument 4 for getTree(), called in /in/KXDOM on line 49 and defined in /in/KXDOM on line 3 Notice: Undefined variable: lastId in /in/KXDOM on line 8 Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 130968 bytes) in /in/KXDOM on line 11
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Warning: Missing argument 4 for getTree(), called in /in/KXDOM on line 49 and defined in /in/KXDOM on line 3 Notice: Undefined variable: lastId in /in/KXDOM on line 8 Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 130968 bytes) in /in/KXDOM on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/KXDOM on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/KXDOM on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/KXDOM on line 6
Process exited with code 255.

preferences:
248.22 ms | 401 KiB | 315 Q