1. 树状结构组装输出tree函数,从根节点0开始寻找
public function getlist()
{
$field = 'category_id,category_pid,category_name';
$order = [
];
$map = [
'status' => 1
];
$cate = $this->field($field)
->where($map)
->select();
// exit;
$list = $this->tree($cate);
return $list;
}
public function tree($cate,$name='child',$category_pid=0)
{
$arr = array();
foreach ($cate as $key => $v) {
if ($v['category_pid'] == $category_pid) {
$arr[] = $v;
$v[$name] = $this->tree($cate,$name,$v['category_id']);
}
}
return $arr;
}