// +---------------------------------------------------------------------- use think\facade\Cache; use sent\tree\Tree; function getDepartmentChild($pid = 0){ $department = Cache::get('department'); if(!$department){ $department = \app\model\auth\Departments::where('status', '=', 1)->column('id,parent_id,title', 'id'); Cache::set('department', $department); } $res = getChilds($department, $pid, 'id', 'parent_id'); $res[] = (int) $pid; //把自己包含在内 return $res; } /** * 获得所有的子 * @param [type] $id [description] * @return [type] [description] */ function getChilds($data, $id = 0, $pk = 'id', $pid = 'parent_id') { $array = []; foreach ($data as $k => $v) { if ($v[$pid] == $id) { $array[] = $v[$pk]; $array = array_merge($array, getChilds($data, $v[$pk])); } } return $array; }