php 无限极分类(两种方式)

第一种:
  
 public function comment() {
        $data = $this->com_list();
        $this->succ('操作成功',$data);
    }

protected  function com_list($pid = 0, &$result = []) {
        $comment    = Db::name('comment')->order('create_time desc')->where(['pid'=>$pid])->select();
        if (empty($comment)) {
            return array();
        }
        foreach ($comment as $k=>$v) {
            $arr = &$result[];
            $v['children'] = $this->com_list($v['id'],$arr);
            $arr = $v;
        }
        return $result;
    }

第二种:

  

  public function comment() {
        $wid        = param_check('wid'); //作品id
        $comment    = Db::name('comment')->order('create_time desc')->where(['wid'=>$wid])->select();
        $data = $this->com_list($comment,0);
        $this->succ('操作成功',$data);
    }

    
    protected  function com_list($data, $pid = 0) {
        $node = [];
        foreach ($data as $k=>$v) {
            if ($pid == $v['pid']) { 
                $node[$k] = $v;
                $node[$k]['children'] = $this->com_list($data,$v ['id']);
            }
        }
        return $node;
    }

 我是tp5框架写的

posted @ 2021-04-20 14:43  我又无所求  阅读(174)  评论(0)    收藏  举报