This article mainly introduces php recursionUsage examples(php recursive function), including recursively obtaining role IDString, recursively obtaining cascade roles Information array, obtain child role information through the id of the parent role. Friends who need it can refer to the recursive function usage of
//递归获得角色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(); }
A function calling itself in its function body is called recursion transfer. This kind of function is called a recursive function. This usually has high practical value for programmers, and is often used to decompose complex problems into simple and identical situations, and do this repeatedly until the problem is solved.
The difference between using recursive functions and not using recursive functions
Example 1: Using static variables
function test(){ static $dig=0; if($dig++<10){ echo $dig; test(); } } test();//12345678910
Example 2: Using recursive functions and loops to implement string reverse arrangement
function unreverse($str){ for($i=1;$i<=strlen($str);$i++){ echo substr($str,-$i,1); } } unreverse("abcdefg");//gfedcbc function reverse($str){ if(strlen($str)>0){ reverse(substr($str,1)); echo substr($str,0,1); return; } } reverse("abcdefg");//gfedcbc
We can often replace recursive functions with loops. It is recommended to use loops when we cannot, because loops are easier to understand and less prone to errors.
php recursive function PHP pays for recursive functions. Recursive functions call themselves. These functions are particularly suitable for browsing dynamic data structures, such as trees and lists.
Almost no web applications require the use of complex data structures
<?php function reversr_r($str) { if (strlen($str)>0) reverse_r(substr($str,1)); echo substr($str,0,1); return; } ?> <?php function reverse_i($str) { for($i=1;$i<=strlen($str);$i++) { echo substr($str,-$i,1); } }
This program listing implements two functions. Both functions can print the contents of the string in reverse order. The function reversr_r is recursively is implemented, and the function reverse_i() is implemented through a loop
The above is the detailed content of Detailed explanation of php recursive function usage examples. For more information, please follow other related articles on the PHP Chinese website!