public function listToTree($list)
{
usort($list,
function($a, $b) {
if ($a['pid'] == $b['pid']) {
return 0;
} elseif ($a['pid'] < $b['pid']) {
return 1;
} else {
return 0;
}
});
$tree=[];
$indexedData=[];
foreach($list as $item){
$indexedData[$item['id']]=$item;
}
foreach($indexedData as $id=>$item){
if($item['pid']==0){
$tree[]=$indexedData[$id];
}else{
$indexedData[$item['pid']]['children'][]=$indexedData[$id];
}
}
return $tree;
}
评论