3v4l.org

run code in 300+ PHP versions simultaneously
<?php $links = [ [ 1, 3], [ 3, 5], [ 3, 7], [ 3, 9], [ 1, 10], [10, 15], ]; $input = array_map(function ($link) { return ['id' => $link[1], 'parent' => $link[0]]; }, $links); function buildTreeMap($input, $child = 'id', $parent = 'parent') { $map = array(); foreach ($input as $node) { // init self if (!array_key_exists($node[$child], $map)) { $map[$node[$child]] = []; } // init parent if (!array_key_exists($node[$parent], $map)) { $map[$node[$parent]] = []; } // add to parent $map[$node[$parent]][$node[$child]] =& $map[$node[$child]]; } return $map; } $map = buildTreeMap($input); // the whole map print_r($map); // only from root 1 print_r($map[1]);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [3] => Array ( [5] => Array ( ) [7] => Array ( ) [9] => Array ( ) ) [1] => Array ( [3] => Array ( [5] => Array ( ) [7] => Array ( ) [9] => Array ( ) ) [10] => Array ( [15] => Array ( ) ) ) [5] => Array ( ) [7] => Array ( ) [9] => Array ( ) [10] => Array ( [15] => Array ( ) ) [15] => Array ( ) ) Array ( [3] => Array ( [5] => Array ( ) [7] => Array ( ) [9] => Array ( ) ) [10] => Array ( [15] => Array ( ) ) )

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:
34.28 ms | 404 KiB | 8 Q