3v4l.org

run code in 300+ PHP versions simultaneously
<?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[$id]) > 0 ) 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 git.master, git.master_jit, rfc.property-hooks
Warning: Undefined array key 0 in /in/dbZ8m on line 7 Warning: Undefined array key 0 in /in/dbZ8m on line 15 Warning: Undefined array key "" in /in/dbZ8m on line 7 Warning: Undefined array key "" in /in/dbZ8m on line 9 Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /in/dbZ8m:9 Stack trace: #0 /in/dbZ8m(15): getTree(Array, Array, NULL) #1 /in/dbZ8m(15): getTree(Array, Array, 0) #2 /in/dbZ8m(15): getTree(Array, Array, 1) #3 /in/dbZ8m(15): getTree(Array, Array, 3) #4 /in/dbZ8m(42): getTree(Array, Array, 4) #5 {main} thrown in /in/dbZ8m on line 9
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
56.28 ms | 401 KiB | 8 Q