限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >PHP笔记 >PHP基础

实例 | 无级分类

分类:PHP基础时间:2018-10-16浏览:2445
$value) {
            if($value['father_id'] == $id)
            {
                $value['depth'] = $depth;
                $tree[] = $value;
                subTree($arr, $value['id'], $depth+1);
                //tree($arr, $value['id'],$depth+1);
            }
        }
        return $tree;
    } 
   
    function fatherTree($arr,$id) { 
        static $list = array(); 
        foreach($arr as $v) { 
            if($v['id'] == $id) { 
                fatherTree($arr,$v['father_id']); 
                $list[] = $v;            
            } 
        } 
        return $list; 
    } 
   
 $arr = [
    ['id'=>1,'name'=>'news','father_id'=>0],
    ['id'=>2,'name'=>'article','father_id'=>0],
    ['id'=>3,'name'=>'local news','father_id'=>1],
    ['id'=>4,'name'=>'good article','father_id'=>2],
    ['id'=>5,'name'=>'bad article','father_id'=>2],
    ['id'=>6,'name'=>'sea','father_id'=>4],
];
 
$tree = subTree($arr);
foreach($tree as $v){
    echo str_repeat('----',$v['depth']).$v['name'].$v['depth'].'
'; } $list = fatherTree($arr,6); foreach ($list as $key => $value) { echo $value['name'].'>>'; }
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.