public function allClassify() { $model = new ClassifyLogic(); $list = $model -> getList('', '', ''); foreach ($list as $k=>$v) { $cat_class[$k]['id'] = $v['id']; $cat_class[$k]['cate_name'] = $v['cate_name']; $cat_class[$k]['pid'] = $v['pid']; $getSon = $this->get_child_tree($v['id'],1); if ($getSon) { $cat_class[$k]['child'] = $this->get_child_tree($v['id']); } } foreach ($cat_class as $v) { if ($v['pid'] != 0) { unset($v); } $new_list[] = $v; } $list= array_filter($new_list); return json_encode([ 'code' => 0, 'response' => $list, 'errMsg' => '请求成功' ]); } //获取当前分类下的子分类(不包括自己) function get_child_tree($tree_id,$get=0) { $two_arr = []; $model = new ClassifyLogic(); $list = $model -> getList(['pid' => $tree_id], '', ''); if($get && empty($list)){ return false; } foreach ($list AS $k=>$v) { $two_arr[$k]['id'] = $v['id']; $two_arr[$k]['cate_name'] = $v['cate_name']; $two_arr[$k]['pid'] = $v['pid']; $getSon = $this->get_child_tree($v['id'],1); if ($getSon){ $two_arr[$k]['child'] = $this->get_child_tree($v['id']); } } return $two_arr; }
来源:oschina
链接:https://my.oschina.net/u/3853452/blog/4287370