其他分享
首页 > 其他分享> > 实现无限级分类

实现无限级分类

作者:互联网

if (!function_exists('get_cate_list')) {
//递归函数 实现无限级分类列表
function get_cate_list($list,$pid=0,$level=0) {
static $tree = array();
foreach($list as $row) {
if($row['pid']==$pid) {
$row['level'] = $level;
$tree[] = $row;
get_cate_list($list, $row['id'], $level + 1);
}
}
return $tree;
}
}

function getCate($list,$pid=0,$level=0){
static $tree = array();
foreach ($list as $row){
if($row['pid'] == $pid){
$row['level'] = $level;
$tree[] = $row;
getCate($list,$row['id'],$level+1);

}
}
return $tree;
}

if(!function_exists('get_tree_list')){
//引用方式实现 父子级树状结构
function get_tree_list($list){
//将每条数据中的id值作为其下标
$temp = [];
foreach($list as $v){
$v['son'] = [];
$temp[$v['id']] = $v;
}
//获取分类树
foreach($temp as $k=>$v){
$temp[$v['pid']]['son'][] = &$temp[$v['id']];
}
return isset($temp[0]['son']) ? $temp[0]['son'] : [];
}
}

标签:temp,level,实现,list,pid,分类,tree,无限,row
来源: https://www.cnblogs.com/qsn17501005/p/15554335.html