ThinkPHP unlimited categories
高洛峰
高洛峰 2017-06-05 11:08:01
0
3
644

How to perform unlimited classification based on id and pid

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
迷茫

Use a recursive function, but the space complexity of this method was not optimized. I forgot the previous way of writing it

private function getTreeList($data, $pid = 0)
    {
        $resultarr = array();
        foreach ($data as $teamdata) {
            if ($teamdata['pid'] == $pid) {
                $team_data = $teamdata;
                $children_data = $this->getTreeList($data, $teamdata['id']);
                $team_data['children'] = $children_data;
                $resultarr[] = $team_data;
            }
        }
        return $resultarr;
    }
左手右手慢动作

Use recursion to loop out an array

phpcn_u1582
public function gettree($items, $parent_id = 'parent_id', $id = 'id'){
    $tree = array(); //格式化好的树
    if(empty($items)){
        return $tree;
    }
    foreach ($items as $item){
        if (isset($items[$item[$parent_id]])){
            $items[$item[$parent_id]]['son'][] = &$items[$item[$id]];
        }else{
            $tree[] = &$items[$item[$id]];
        }
    }
    return $tree;

}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!