数据表结构

//递归 - 获取当前分类下的所有子集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
)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号