From flat array:

$tmp = array( array('id' => 0, 'level' => 0, 'name' => 'root', 'left' => 1, 'right' => 13), array('id' => 1, 'level' => 1, 'name' => 'ะขะตั…ะพั‚ะดะตะป', 'left' => 2, 'right' => 8), array('id' => 2, 'level' => 1, 'name' => 'ะžั‚ะดะตะป ะบะฐะดั€ะพะฒ', 'left' => 9, 'right' => 10), array('id' => 3, 'level' => 1, 'name' => 'ะญะบะพะฝะพะผะธั‡ะตัะบะธะน ะพั‚ะดะตะป', 'left' => 11, 'right' => 12), array('id' => 4, 'level' => 2, 'name' => 'ะŸะพะดะพั‚ะดะตะป ั‚ะตั…ะพั‚ะดะตะปะฐ 1', 'left' => 3, 'right' => 6), array('id' => 5, 'level' => 2, 'name' => 'ะŸะพะดะพั‚ะดะตะป ั‚ะตั…ะพั‚ะดะตะปะฐ 2', 'left' => 7, 'right' => 8), array('id' => 6, 'level' => 3, 'name' => 'ะŸะพะดะฟะพะดะพั‚ะดะตะป ั‚ะตั…ะพั‚ะดะตะปะฐ 1', 'left' => 4, 'right' => 5), ); 

You need to build a tree view:

 $tree = array( array( 'id' => 0, 'level' => 0, 'name' => 'root', 'children' => array( array( 'id' => 1, 'level' => 1, 'name' => 'ะขะตั…ะพั‚ะดะตะป', 'children' => array( array( 'id' => 4, 'level' => 2, 'name' => 'ะŸะพะดะพั‚ะดะตะป ั‚ะตั…ะพั‚ะดะตะปะฐ 1', 'children' => array( array( 'id' => 6, 'level' => 3, 'name' => 'ะŸะพะดะฟะพะดะพั‚ะดะตะป ั‚ะตั…ะพั‚ะดะตะปะฐ 1' ), ), ), array( 'id' => 5, 'level' => 2, 'name' => 'ะŸะพะดะพั‚ะดะตะป ั‚ะตั…ะพั‚ะดะตะปะฐ 2' ), ), ), array( 'id' => 2, 'level' => 1, 'name' => 'ะžั‚ะดะตะป ะบะฐะดั€ะพะฒ' ), array( 'id' => 3, 'level' => 1, 'name' => 'ะญะบะพะฝะพะผะธั‡ะตัะบะธะน ะพั‚ะดะตะป' ), ) ), ); 

Breaking my head, I can not figure out how to do it.

  • explain what you mean here left and right ? - teran
  • and generally the principle of constructing such a tree on the available data. I do not see any connection. An example because the source data and the result tree correspond to each other? where information about the parent node is stored, or about descendants? So far, only the level hierarchy is clear - teran
  • one
    @teran these keys organize storage of hierarchical data structures in a relational database - getinfo.ru/article610.html - Denis Nesteruk September
  • and, most importantly, I missed the headline (: - teran

1 answer 1

Probably, the solution algorithm does not exist alone, so a simple variant of construction:

First, we define the maximum depth and decompose the array into levels:

 $lmax = max(array_column($tmp, 'level')); $levels = array_fill(0, $lmax, []); foreach($tmp as $node){ $levels[$node['level']][] = $node + ['children' => []]; } 

Now we define the construction function itself. At the entrance you need a parent node. We will sort through the elements that lie on the level below. If the bottom element fits under the boundary conditions, then add it to the children . Next, run the recursion.

 $build = function(&$root) use (&$build, $levels){ $l = $root['level'] + 1; if(!isset($levels[$l])) return; foreach($levels[$l] as &$n){ if(($n['id'] >= $root['left']) && ($n['id'] <= $root['right'])){ $root['children'][] = &$n; $build($n); } } }; 

Attention should be paid to the use of references & both in the looping cycle and when added to children

It remains to run this thing.

 $root = $levels[0][0]; $build($root); print_r($root); 

The output is the desired structure.

In principle, you can simultaneously remove items from the $levels that we bring in children so that they will not be sorted out the next time (in recursion).

  • I Shmagla)), corrected a little code, because I deal with php 5.3.13 (:, freaked out globals and it all worked. Thank you very much! - Denis Nesteryuk September