3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $category = array( array('left' => 1, 'right' => 11, 'x' => 'A'), array('left' => 2, 'right' => 4, 'x' => 'B'), array('left' => 5, 'right' => 10, 'x' => 'C'), array('left' => 6, 'right' => 7, 'x' => 'D'), array('left' => 8, 'right' => 9, 'x' => 'E'), array('left' => 12, 'right' => 13, 'x' => 'F'), ); function createTree ($category, $left = 0, $right = null) { $tree = array(); foreach ($category as $cat => $data) { if ($data['left'] == $left + 1 && (is_null($right) || $data['right'] < $right)) { $tree[$cat] = createTree($category, $data['left'], $data['right']); $left = $data['right']; } } return $tree; } $tree = createTree ($category); print_r($tree);
Output for 4.3.0 - 7.2.0
Array ( [0] => Array ( [1] => Array ( ) [2] => Array ( [3] => Array ( ) [4] => Array ( ) ) ) [5] => Array ( ) )