首页 > 开发 > PHP > 正文

php递归使用示例(php递归函数)

2024-05-04 23:21:16
字体:
来源:转载
供稿:网友

复制代码 代码如下:


//递归获得角色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();
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表