表结构
垃圾分类表:
分类内容表:
查询代码:
//垃圾分类内容
public function get_content()
{
//获取当前分类id
$id = I('get.id', '');
$model = M();
$sql = "select id,parent_id,path,content from rs_rubbish_content where cid = " . $id . " ";
$res = $model->query($sql); //查询所有
//回调函数
$list = $this->data_format($res, 0, 0);
$this->apiReturn($list, 0, "success");
}
//垃圾内容递归查询
private function data_format($arr, $level = 0, $id = 0)
{
$list = [];
foreach ($arr as $vo) {
if ($vo['parent_id'] == $id) {
//$vo['rank']=$level;
$temp_list = $this->data_format($arr, $level + 1, $vo['id']);
if (!empty($temp_list)) {
$sort_arr = array_column($temp_list, 'sort');
array_multisort($sort_arr, SORT_ASC, $temp_list);
}
$vo['children'] = $temp_list;
$list[] = $vo;
}
}
return $list;
}
查询出来的结果: