PHP 找出所有上级部门 ThinkPHP6框架
先一次查出所有部门
再递归出判断出全部上层部门
// 获取部门上级ID
public function GetParentDepartments($dep_id)
{
$result = Db::table("department")->field("id,pid,name")->select()->toArray();
return getParentss($result, $dep_id);
}
public function getParentss($array, $p_id)
{
$sups = array();
foreach ($array as $item) {
if ($item['id'] == $p_id) {
$sups[] = $item;
$sups = array_merge($sups, getParentss($array, $item['pid']));
}
}
return $sups;
}
调用
传参部门IDGetParentDepartments(100);