左侧树无限层级算法

/*
* 暂时没想到好的算法,先这样
*
*/

public function getDpInfoAllByTreeGet(){
$rs = $this->_getDpInfoAllByTree();
$rs = $this->_getDpInfoAllByTreeGet($rs);
$arr['title'] = '已授权用户';
$arr['id'] = '';
$arr['level'] = 0;
$arr['dep_no'] = '';
array_unshift($rs,$arr);
$arr['children'] = $rs;
return $rs;


}


//树形结构算法优化

public function _getDpInfoAllByTreeGet(&$rs){
foreach ($rs as $key => &$val){
if(isset($val['children'])){
$this->_changeStructure($val['children']);
$this->_getDpInfoAllByTreeGet($val['children']);
}
}
return $rs;
}




/*
* 没有用户的部门树信息,左侧菜单栏数据
*/
private function _getDpInfoAllByTree(){
$rsDp = DB::table('department')
->select('dep_name as title','id','level','dep_no')
->orderBy('dep_no', 'asc','level')
->get()
->toArray();


// var_dump($rsDp);exit;

$level = DB::table('department')->max('level');//从数据库获取最大层数
for ($i = 0; $i < $level; $i++) {
$rsDp = $this->_getDpInfoAll($rsDp);
}
foreach ($rsDp as $key => $val) {
if ($val['level'] != 1) {
unset($rsDp[$key]);
}
}
sort($rsDp);
return $rsDp;
}


private function _getDpInfoALl($rsDp){
$data = $rsDp;
foreach ($rsDp as $key => $val) {
for ($i = $key+1; $i < count($rsDp); $i++) {
if (strpos($rsDp[$i]['dep_no'], $rsDp[$key]['dep_no']) !== FALSE && $rsDp[$i]['level'] == ($rsDp[$key]['level'] + 1)) {
$now_level = $rsDp[$key]['level'] + 1;
$data[$key]['children'][$now_level][$rsDp[$i]['dep_no']] = $rsDp[$i];
}
}
}
return $data;
}

posted on 2018-06-05 17:41  铁轨嘴上飘  阅读(140)  评论(0编辑  收藏  举报

导航