获取树形数据(区域,父子级关系的树形数据)

public function getAreaTree($code)
{
$where = '';
if ($code){
$where = ['b.code' => $code];
}
$info = Db::table('__AREA__ a')
->join('__AREA__ b','a.p_code = b.code','left')
->field('a.id,a.code as v,a.name as n,a.p_code,b.name as p_name')->where($where)->select();
if(!empty($info)){
$info = array_column($info,null,'v');
$info = $this->generateTreeback($info);
}
return $info;
}

function generateTreeback($items,$field = 'v', $pfield = 'p_code', $child = 'c')
{
$tree = [];
foreach($items as $item){
if(isset($items[$item[$pfield]])){
$items[$item[$pfield]][$child][] = &$items[$item[$field]];
}else{
$tree[] = &$items[$item[$field]];
}
}
return $tree;
}
---------------------
作者:是后海大鲨鱼
来源:CSDN
原文:https://blog.csdn.net/qq_36647209/article/details/88824287
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2019-03-26 17:00  后海大鲨鱼、  阅读(641)  评论(0编辑  收藏  举报