左侧树无限层级算法递归版,今天想好了。。
private function _getAllDep(){ $rsDp = DB::table('department') ->orderBy('dep_no', 'asc') ->get() ->toArray(); return $rsDp; } public function getTree(){ $rsDp = $this->_getAllDep(); $rsDp = $this->_getTree($rsDp,$rsDp,$rsDp); sort($rsDp); return $rsDp; } private function _getTree(&$rsDp,$rsDpAll,&$rsFirst) { foreach ($rsDp as $key => &$val) { foreach ($rsDpAll as $key1 => $val1) { if (isset($val1['dep_no']) && isset($val['dep_no']) && strpos($val1['dep_no'], $val['dep_no']) !== FALSE && $val1['level'] == ($val['level'] + 1)) { $val['children'][] = $val1; unset($rsFirst[$key1]); } } if(!empty($val['children'])){ $this->_getTree($val['children'],$rsDpAll,$rsFirst); } } return $rsDp; }