3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function getTree($treeByParent, $treeByCat, $id, $listed = []) { $gTree = []; $gTree[] = $treeByCat[ $id ]; if ( count($treeByParent[ $id ]) > 0) { foreach ($treeByParent[ $id ] as $cat) $gTree[] = $cat; } if ( count($treeByParent[ $treeByCat[ $id ] ]) > 0 ) $gTree[] = getTree($treeByParent, $treeByCat, $treeByCat[ $id ]); return $gTree; } $treeByParent = array( 0 => array(1, 2), 1 => array(3, 7), 3 => array(4), 4 => array(5) ); $treeByCat = array( 1 => 0, 2 => 0, 3 => 1, 7 => 1, 4 => 3, 5 => 4 ); print_r( getTree($treeByParent, $treeByCat, 4) );
Output for 5.4.0 - 7.1.0
Notice: Undefined offset: 0 in /in/qFpgo on line 7 Notice: Undefined offset: 0 in /in/qFpgo on line 15 Notice: Undefined index: in /in/qFpgo on line 15 Array ( [0] => 3 [1] => 5 [2] => Array ( [0] => 1 [1] => 4 [2] => Array ( [0] => 0 [1] => 3 [2] => 7 [3] => Array ( [0] => [1] => 1 [2] => 2 ) ) ) )
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/qFpgo on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/qFpgo on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/qFpgo on line 3
Process exited with code 255.