## 递归查询 其中 pid是id的父级别 相互对应 ### 例子1 ~~~ public function getcatebypid($pid) { $data = Db::table("cates")->where("pid", $pid)->select(); $res = []; //遍历 递归 foreach ($data as $key => $value) { // 代码段:核心点 $value['shop'] = $this->getcatebypid($value['id']); $res[] = $value; } return $res; } ~~~ ### 例子2 ~~~ function getList($brr, $pid = 0, &$arr = [], $level = 1) { foreach ($brr as $v) { if ($v['pid'] == $pid) { $v['level'] = $level; $arr[] = $v; getList($brr, $v['id'], $arr, $level + 1); } } return $arr; } ~~~ ### 例子3 将列表转化为树状 ~~~ static function listToTree($list,$root_id = 0){ $arr = []; foreach($list as $item){ if ($item->pid == $root_id) { $children = self::listToTree($list, $item->id); if($children){ $item->children = $children; } $arr[] = $item; } } return $arr; } ~~~ 调用1 其中0是pid 表示获取0下所有的子级别数据 `getcatebypid(0)` 调用2 ~~~ getList(db("user)->select(),$user['role_id']); {:str_repeat('|——',$vo.level)}{$vo.name} //tp5模板用法 ~~~