php recursive functionis an indispensable part in our actual development. For programmers, the php recursive function has high usage value and can solve many problems. Today we will introduce it to you. Let’s take a look at theusage examples of PHP recursive functions!
This article mainly introduces examples of using PHP recursion (php recursive function), including recursively obtaining the role IDString, recursively obtaining the cascade role information array, and obtaining the id of the parent role For sub-role information, friends who need it can refer to
//递归获得角色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's recursive function usage
A function calling itself within its function body is called a recursive call. 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 Looping to implement string reversal permutation
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
Recursive functions can often be replaced by loops. It is recommended to use loops when we cannot replace them, because loops are easier to understand and less prone to errors.
php recursive function PHP pays recursive functions. Recursive functions call themselves. These functions are especially suitable for browsing dynamic data structures, such as trees and lists.
Almost no web applications require the use of complex data structures
0) reverse_r(substr($str,1)); echo substr($str,0,1); return; } ?>Copy after login
This program listing implements two functions, both of which can print the contents of a string in reverse order
The function reversr_r is It is implemented through recursion, while the function reverse_i() is implemented through loops
Summary:
This article uses two examples to analyze The use of PHP recursive functions, friends can have a substantial understanding of PHP recursive functions through this article!
Related recommendations:
What is PHP recursive function and simple example explanation
How to use php recursive function effectively? Typical examples of php recursive functions
Do you know the precautions for using return in php recursive functions
The above is the detailed content of Example of using php recursive function. For more information, please follow other related articles on the PHP Chinese website!