/***获取树形结构*User:JIANGJJ<xpxw@qq.com>*@paramarray$result*@returnarray*/publicstaticfunctiongetTree($result){$items=[];fo...
/** * 获取树形结构 * User: JIANGJJ<xpxw@qq.com> * @param array $result * @return array */ public static function getTree($result) { $items = []; foreach ($result as $v) { $items[$v['id']] = $v; } $tree = []; foreach ($items as $k => $item) { if (isset($items[$item['pid']])) { $items[$item['pid']]['child'][] = &$items[$k]; } else { $tree[] = &$items[$k]; } } return $tree; } /** * 获取全部子级节点 * User: JIANGJJ<xpxw@qq.com> * @param $pid * @param $cid * @return array */ static function getChilds($pid, $cid) { $allData = self::getByCid($cid); $map = []; foreach ($allData as $key => $item) { $map[] = $item['pid'] . '_' . $key . '_' . $item['id']; } $data[] = $pid; foreach ($map as $mix_str) { $mix = explode('_', $mix_str); if (in_array(intval($mix[0]), $data)) { $result[] = $allData[intval($mix[1])]; $data[] = intval($mix[2]); } } return $result; } /** * 获取指定企业 指定上组织架构 * User: JIANGJJ<xpxw@qq.com> * @param $pid * @param $cid * @return array */ static function getParents($pid, $cid) { $result = self::getByCid($cid); $result = array_combine(array_column($result, 'id'), $result); $data = []; $arr = ['pid' => $pid]; while (isset($result[$arr['pid']]) and $arr = $result[$arr['pid']]) { $data[] = $arr; } return $data; } /** * 获取企业的组织架构树 * User: JIANGJJ<xpxw@qq.com> * @param int $cid * @return array */ public static function Department($cid = 0) { $result = self::orderBy('id', 'ASC')->with('rank:id,department_id,title,is_manager,is_sub_manager')->where('company_id', $cid)->whereNull('deleted_at')->get(['id', 'company_id', 'pid', 'title'])->toArray(); $items = []; foreach ($result as $v) { $items[$v['id']] = $v; } $tree = []; foreach ($items as $k => $item) { if (isset($items[$item['pid']])) { $items[$item['pid']]['child'][] = &$items[$k]; } else { $tree[] = &$items[$k]; } } return $tree; }
全文详见:http://xpxw.com/?id=148