引用式(性能更好)
- 引用式无限极分类
- 必须存在主键id 必须存在父级 pid
function getTreeList($data){
$list=[];
foreach ($data as $v){
$id=$v['id'];
$list[$id]=$v;
}
$tree=[];
foreach ($list as $k=>$v){
$pid=$v['pid'];
if(isset($list[$pid])){
$list[$pid]['children'][]=&$list[$k];
}else{
$tree[$v['id']]=&$list[$k];
}
}
return $tree;
}
递归式
递归的方式就不多说了,直接上代码,用心领会吧
function tree(&$list,$pid=0,$level=0,$html='  ')
{
static $tree = array();
foreach($list as $v){
if($v['pid'] == $pid){
$v['sort'] = $level;
$v['html'] = str_repeat($html,$level);
$tree[] = $v;
tree($list,$v['role_id'],$level+1);
}
}
return $tree;
}
网友评论