Thinkphp5栏目管理的无限极分类

模型层代码:

namespace app\admin\model;

use think\Model;

 

class Cate extends Model

{

    public functioncatetree(){

        $cateres =$this->select();

        return$this->sort($cateres);

    }

 

    public functionsort($data,$pid=0,$level=0){

        static $arr = array();

        foreach ($data as $k=> $v) {

           if($v['pid']==$pid){

                $v['level'] =$level;

                $arr[] = $v;

               $this->sort($data,$v['id'],$level+1);

            }

        }

        return $arr;

    }

}

注意:递归函数中定义数组用的是静态数组,因为要重复调用。

你可能感兴趣的:(thinkphp5)