Without recursive implementation of the infinite level of classification, a simple test of performance than recursion a little bit better, but write too complex, or recursive simple convenient point
Code:
<?php $list = array (' ID ' =>1, ' pid ' =>0, ' deep ' =>0, ' name ' => ' test1 '), array (' ID ' =>2, ' pid ' => 1, ' Deep ' =>1, ' name ' => ' test2 '), array (' ID ' =>3, ' pid ' =>0, ' deep ' =>0, ' name ' => ' test3 '), array (' ID ' =& Gt;4, ' pid ' =>2, ' deep ' =>2, ' name ' => ' test4 '), array (' ID ' =>5, ' pid ' =>2, ' deep ' =>2, ' name ' => ' Test5 ') ), array (' ID ' =>6, ' pid ' =>0, ' deep ' =>0, ' name ' => ' Test6 '), array (' ID ' =>7, ' pid ' =>2, ' deep ' =>2, ' nam E ' => ' test7 '), array (' ID ' =>8, ' pid ' =>5, ' deep ' =>3, ' name ' => ' Test8 '), array (' ID ' =>9, ' pid ' =>3, ' de
EP ' =>2, ' name ' => ' Test9 ');
Function Resolve ($list) {$newList = $manages = $deeps = $inDeeps = Array ();
foreach ($list as $row) {$newList [$row [' id ']] = $row;
} $list = null; foreach ($newList as $row) {if (! isset ($manages [$row [' pid ']]) | |! isset ($manages [$row [' pid ']][' Children '] [$row [' ID '] ]) {if ($row [' pid '] > 0 &&! isset ($manages [$row [' pid ']][' Children '])
$manages [$row [' pid ']] = $newList [$row [' pid ']];
$manages [$row [' pid ']][' Children '] [$row [' id ']] = $row; } if (! isset ($inDeeps [$row [' deep ']]) | |! In_array ($row [' id '], $inDeeps [$row [' deep ']]) {$inDeeps [$row [' Deep ']
[] = Array ($row [' pid '], $row [' id ']);
} krsort ($inDeeps);
Array_shift ($inDeeps); foreach ($inDeeps as $deep => $ids) {foreach ($ids as $m) {//The presence of a sub column for transfer if (Isset ($manages [$m [1]])
{$manages [$m [0]][' Children '] [$m [1]] = $manages [$m [1]];
$manages [$m [1]] = NULL;
Unset ($manages [$m [1]]);
}} return $manages [0][' Children ']; }
Recursive implementation
Function Resolve2 (& $list, $pid = 0) {
$manages = array ();
foreach ($list as $row) {
if ($row [' pid '] = = $pid) {
$manages [$row [' id ']] = $row;
$children = Resolve2 ($list, $row [' id ']);
$children && $manages [$row [' id ']][' children '] = $children;
}
}
return $manages;
}
The above PHP does not use recursive infinite class simple example is small series to share all the content, hope to give you a reference, also hope that we support cloud habitat community.