无限级分类
递归方式
private function wuxianfenlei($data,$id=0,$lev=0){
$son=array();
foreach ($data as $k=>$v){
if($v['parentid']==$id){
$v['lev']=$lev;
$son[]=$v;
unset($data[$k]); //把这个节点从数组中移除,减少后续递归消耗
$this->wuxianfenlei($data,$v['cateid'],$lev+1);
}
}
return $arr;
}
$array = getTree($array);
foreach($array) as $value{
echo str_repeat('--', $value['level']), $value['name'].'<br />';
}
引用方式
private function yinyong($data,$pid=0){
$items=array();
foreach ($data as $value){
$items[$value['cateid']]=$value;
}
$tree=array();
foreach ($items as $value) {
if ($pid == $value['parentid']) {//先取出顶级
$tree[] = &$items[$value['cateid']];
} elseif (isset($items[$value['parentid']])) {//再判定非顶级的pid是否存在,如果存在,则再pid所在的数组下面加入一个字段items,来将本身存进去
$items[$value['parentid']]['items'][] = &$items[$value['cateid']];
}
}
print_var($tree);
}