Home >Backend Development >PHP Tutorial >php递归,该如何处理

php递归,该如何处理

WBOY
WBOYOriginal
2016-06-13 11:15:03938browse

php递归
有一个数据表,结构如下:
id      classid       zengclassid     createdate

需要递归查出classid=$id的zengclassid,还得判断zengclassid如果也有赠送的课程时,需要把这条记录也查出来,
例如:
1        470           445             2010-01-01  11:11:11
2        470           448             2010-01-01  11:11:11
3        448           457             2010-01-01  11:11:11
4        445           490             2010-01-01  11:11:11

如果查找id为470的结果,那么需要查出:445,448,457,490;

我的代码递归得有问题,请大虾们赐教一下
function getVideo_zs($classid,$arr){
global $dsql;
$sql = "SELECT zengclassid FROM course WHERE classid=".$classid;
$dsql->Execute('mme',$sql);
while($rs=$dsql->getArray('mme')){
$arr[] = $rs['zengclassid'];
if(!empty($rs['zengclassid'])){
$arr = getVideo_zs($rs['zengclassid'],$arr);
}
}
        
           return $arr; 
}
$zs_list = array();
$zs_zrr = getVideo_zs(470,$zs_list);   


------解决方案--------------------
$arr[] = $rs['zengclassid'];//累积结果
if(!empty($rs['zengclassid'])){
$arr = getVideo_zs($rs['zengclassid'],$arr);//怎么递归一下就覆盖呢?

另外 $dsql->Execute('mme',$sql); 后,资源在 $dsql 内
请确认结果集不会被覆盖
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn