当前位置: 编程技术>php
php递归使用示例(php递归函数)
来源: 互联网 发布时间:2014-08-26
本文导语: //递归获得角色ID字符串 function explodeRole($roleObj, &$resultStr){ if(0 < count($roleObj->childRoleObjArr)){ foreach($roleObj->childRoleObjArr as $childRoleObj){ if('' == $resultStr){ $resultStr .= "{$childRoleObj->id}"; }else{ $resultStr .= ",...
//递归获得角色ID字符串
function explodeRole($roleObj, &$resultStr){
if(0 < count($roleObj->childRoleObjArr)){
foreach($roleObj->childRoleObjArr as $childRoleObj){
if('' == $resultStr){
$resultStr .= "{$childRoleObj->id}";
}else{
$resultStr .= ", {$childRoleObj->id}";
}
explodeRole($childRoleObj, $resultStr);
}
}
}
//递归获取级联角色信息数组
function makeRoleRelation(&$roleObjArr){
foreach($roleObjArr as $item){
$item->childRoleObjArr = getRoleObjArrByParentId($item->id);
if(0 < count($item->childRoleObjArr)){
makeRoleRelation($item->childRoleObjArr);
}
}
}
//通过父角色的id获取子角色信息
function getRoleObjArrByParentId($parentid){
$operCOGPSTRTSysRole = new COGPSTRTSysRole();
$operCOGPSTRTSysRole->setColumn($operCOGPSTRTSysRole->getAllColumn());
$operCOGPSTRTSysRole->setWhere("parentroleid={$parentid}");
$roleObjArr = $operCOGPSTRTSysRole->convResult2ObjArr($operCOGPSTRTSysRole->selectTable());
return isset($roleObjArr)?$roleObjArr:array();
}
php的递归函数用法
一个函数在它的函数体内调用它自身称为递归调用。这种函数称为递归函数。这对于程序员来说,通常有很高的实用价值,常用来将复杂的问题分解为简单的并相同的情况,反复做这种处理直到问题解决。
用递归函数与不用递归函数的区别
示例一:使用静态变量
function test(){
static $dig=0;
if($dig++