Without recursive implementation of infinite class classification, a simple test of performance than recursion slightly better, but the writing is too complex, or recursive simple convenient point
Code:
<?php$list=Array(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 '=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,' name '=' Test7 '),Array(' id '=8,' pid '=5,' Deep '=3,' name '=' Test8 '),Array(' id '=9,' pid '=3,' Deep '=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) {//There are sub-columns to be transferred 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;}
PHP does not use recursive infinite class classification