原数组为一维数组,含有父级ID,根据父级id做分级
第一个,将一维数组转换为多维数组()
public function sort(&$list,$parent_id=0){
$arr = array();foreach ($list as $key => $v) {
if ($v['parent_id'] == $parent_id) {
$abc[$key] = $v;
$abc[$key]['son']=$this->sort($list,$v['cat_id']);
}
}
return $arr;die;
}
第二个,将原数组分出层级,并用多个‘--’表示
function tree(&$list,$pid=0,$level=0,$html='--'){
static $tree = array();
foreach($list as $v){
if($v['parent_id'] == $pid){
$v['sort'] = $level;
$v['html'] = str_repeat($html,$level);
$tree[] = $v;
$this->tree($list,$v['cat_id'],$level+1);
}
}
return $tree;
}