<?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);
preferences:
49.42 ms | 402 KiB | 5 Q