Blogger Information
Blog 43
fans 2
comment 2
visits 113224
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php 获取分类下所有的子集
朝游东海
Original
5138 people have browsed it

数据表结构

QQ截图20190211171543.png

   //递归 - 获取当前分类下的所有子集id

public function test(){
      $child =  $this->getAllNextId(4);
      p($child);exit;

}


实例

public function getAllNextId($id,$data=[]){
    $pids = DB::name('Category')->where('pid',$id)->column('id');
    if(count($pids)>0){
        foreach($pids as $v){
            $data[] = $v;
            $data = $this->getAllNextId($v,$data); //注意写$data 返回给上级
        }
    }
    if(count($data)>0){
            return $data;
        }else{
            return false;
        }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

当查询id=4时;

得出结果

Array
(
   [0] => 5
   [1] => 6
   [2] => 7
   [3] => 8
   [4] => 9
)


当查询id=10是

Array
(
   [0] => 11
   [1] => 12
   [2] => 13
   [3] => 14
   [4] => 15
   [5] => 16
   [6] => 17
)


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post