PHP列表转树非递归

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;
    }

评论

(= ̄ω ̄=)··· 暂无内容!

回复

您还未登录,请先登录或者注册