php 递归实现无限极分类

public function getTree($data, $pId)
    {
        $tree = [];
        foreach($data as $k => $v)
        {
            if($v['parentid'] == $pId)
            {        //父亲找到儿子
                $v['parentid'] = $this->getTree($data, $v['id']);
                $tree[] = $v;
                //unset($data[$k]);
            }
        }
        return $tree;
    }

使用方法:

public function productAlbum_get(){
        $list = $this->db->select('*')->from('hh_catgory')->get()->result_array();
        $a = $this->getTree($list,0);

        $this->response($a,200);

    }

猜你喜欢

转载自www.cnblogs.com/photo520/p/10207333.html