| 递归数据库
 
  递归的思路就是【找sons】,也就是循环所有数据,找到每条数据的所有sons、sons的sons、sons的sons的sons…,首先我们知道要找test1的sons就是找所有pid为1的数据,于是遍历整个数组找到了test1-1和test1-2;然后还要分别找test1-1和test1-2的sons,就这样一直找下去,由于每次找sons的方法都是一样的,就是遍历所有数据(除开自己的父级,因为父级不可能是sons),找出符合条件的,唯一不同的就是每次找sons的父级不一样,代码如下:
 //查到数据public function CourseCategoryList(){
 $list = DB::connection(‘sqlsrv’)->table(‘demo’)
 ->select(‘Fid’,‘FCategoryName’,‘FParentID’)
 ->get()->toArray();
 t
       
       
        e
       
       
        m
       
       
        p
       
       
        =
       
       
        [
       
       
        ]
       
       
        ;
       
       
        f
       
       
        o
       
       
        r
       
       
        e
       
       
        a
       
       
        c
       
       
        h
       
       
        (
       
      
      
       temp = []; foreach (
      
     
    temp=[];foreach(list as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (comment->FParentID == 0) {
 unset(
    
     
      
       
        l
       
       
        i
       
       
        s
       
       
        t
       
       
        [
       
      
      
       list[
      
     
    list[key]);
 $comment->childrenList = 
    
     
      
       
        t
       
       
        h
       
       
        i
       
       
        s
       
       
        ?
       
       
        >
       
       
        g
       
       
        e
       
       
        t
       
       
        S
       
       
        u
       
       
        b
       
       
        T
       
       
        r
       
       
        e
       
       
        e
       
       
        (
       
      
      
       this->getSubTree(
      
     
    this?>getSubTree(list, $comment->Fid);
 $temp[] = $comment;
 }
 }
 return $this->_response([‘list’ => $temp]);
 }
 public function getSubTree($list, $pid){//初始化sons
 c
       
       
        h
       
       
        i
       
       
        l
       
       
        d
       
       
        =
       
       
        [
       
       
        ]
       
       
        ;
       
       
        /
       
       
        /
       
       
        循
       
       
        环
       
       
        所
       
       
        有
       
       
        数
       
       
        据
       
       
        找
       
      
      
       child = []; //循环所有数据找
      
     
    child=[];//循环所有数据找id的sons
 foreach ($list as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (datum->FParentID == $pid) {
 //保存下来,然后继续找sons的sons
 c
       
       
        h
       
       
        i
       
       
        l
       
       
        d
       
       
        [
       
      
      
       child[
      
     
    child[datum->Fid] = 
    
     
      
       
        d
       
       
        a
       
       
        t
       
       
        u
       
       
        m
       
       
        ;
       
       
        /
       
       
        /
       
       
        先
       
       
        去
       
       
        掉
       
       
        自
       
       
        己
       
       
        ,
       
       
        自
       
       
        己
       
       
        不
       
       
        可
       
       
        能
       
       
        是
       
       
        自
       
       
        己
       
       
        的
       
       
        s
       
       
        o
       
       
        n
       
       
        s
       
       
        u
       
       
        n
       
       
        s
       
       
        e
       
       
        t
       
       
        (
       
      
      
       datum; //先去掉自己,自己不可能是自己的sons unset(
      
     
    datum;//先去掉自己,自己不可能是自己的sonsunset(list[$key]);
 //递归找,并把找到的sons放到一个child的字段中
 c
       
       
        h
       
       
        i
       
       
        l
       
       
        d
       
       
        [
       
      
      
       child[
      
     
    child[datum->Fid]->sons = 
    
     
      
       
        t
       
       
        h
       
       
        i
       
       
        s
       
       
        ?
       
       
        >
       
       
        g
       
       
        e
       
       
        t
       
       
        S
       
       
        u
       
       
        b
       
       
        T
       
       
        r
       
       
        e
       
       
        e
       
       
        (
       
      
      
       this->getSubTree(
      
     
    this?>getSubTree(list, $datum->Fid);
 }
 }
 return $child;
 }
 返回如下: 
 |