TP5无限级分类排序代码

public function tree(){
    $res = $this->select();
    return $this->sort($res);
}
//$data  要排序的数组
//$pid 父级ID,默认0
//$level 分类等级,默认从0开始
public function sort($data,$pid=0,$level=0){
    static $arr = [];
    foreach($data as $v){
        if($v['pid'] == $pid){
            $v['level'] = $level;
            $arr[] = $v;
            $level++;
            $this->sort($data,$v['id'],$level);
        }       
    }
    return $arr;
}

你可能感兴趣的:(thinkphp5)