PHP递归无限分类

function fenleipaixu(){
    $sql = "SELECT cat_id,cat_name,parent_id from ecs_category";
    $res = $GLOBALS['db']->getAll($sql);
    $digui_class = tree_cat($res); 
        $new_class = array();
        $i = 0;
        foreach ($digui_class as $value) {
            $new_class[$i]['cat_id'] = $value['cat_id'];
            $new_class[$i]['cat_name'] = $value['cat_name'];
            $new_class[$i]['parent_id'] = $value['parent_id'];
            $i++;

            if($value['z_class']){
                foreach ($value['z_class'] as  $value_a) {

                    $new_class[$i]['cat_id'] = $value_a['cat_id'];
                    $new_class[$i]['cat_name'] = '  '.$value_a['cat_name'];
                    $new_class[$i]['parent_id'] = $value_a['parent_id'];
                    $i++;

                    if($value_a['z_class']){
                        foreach ($value_a['z_class'] as $value_b) {
                            $new_class[$i]['cat_id'] = $value_b['cat_id'];
                            $new_class[$i]['cat_name'] = '    '.$value_b['cat_name'];
                            $new_class[$i]['parent_id'] = $value_b['parent_id'];
                            $i++;

                        }

                    }
                }
            }
            
        }
        return $new_class;
}

function tree_cat($array,$pid = 0){  
$temp = array();  
foreach ($array as $v) {  
    if ($v['parent_id'] == $pid) {  
        $v['z_class'] = tree_cat($array,$v['cat_id']);  
        $result = count($v['z_class']);
        if($result < 1){
            unset($v['z_class']);
        }
        $temp[] = $v;  
    }  
}  
return $temp;  
} 

$new_fenlei_list = fenleipaixu();
$smarty->assign('new_fenlei_list', $new_fenlei_list);
foreach ($new_fenlei_list as $value) {
    echo $value['cat_name'] .'
'; }

PHP递归无限分类_第1张图片PHP递归无限分类_第2张图片

你可能感兴趣的:(PHP)