zoukankan      html  css  js  c++  java
  • 无极分类之子孙树、父辈树

    无极分类之子孙树、父辈树

    <?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'].'>>';
    }
      
      
      
    
  • 相关阅读:
    webservice
    AppDomain (转)
    Apache和Nginx防盗链的几种配置方法
    优化PHP代码的40条建议
    file_get_contents无法请求https连接的解决方法
    PHP SPL
    Ubuntu 查看系统信息
    PHP导出Excel
    mysql集群
    配置yum源的两种方法
  • 原文地址:https://www.cnblogs.com/fenle/p/4770108.html
Copyright © 2011-2022 走看看