這篇文章主要介紹了php遞迴使用樣本(php遞迴函式),包括遞迴獲得角色ID字串、遞迴擷取級聯角色資訊數組、通過父角色的id擷取子角色資訊,需要的朋友可以參考下
代碼如下://遞迴獲得角色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();}