php 获取分类下所有的子集
程序员文章站
2022-03-01 18:09:02
...
数据表结构
//递归 - 获取当前分类下的所有子集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
)