欢迎光临
感谢一路有你

实例 | 无级分类

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 
<?php
    function subTree($arr, $id=0, $depth=0)
    {
        static $tree = array();
        foreach ($arr as  $key=>$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'].'<br>';
}
  
$list = fatherTree($arr,6);
 
foreach ($list as $key => $value) {
    echo $value['name'].'>>';
}

 

赞(1) 打赏
未经允许不得转载:王明昌博客 » 实例 | 无级分类
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮