Home  >  Article  >  Backend Development  >  Example of using php recursion (php recursive function)_PHP tutorial

Example of using php recursion (php recursive function)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:03768browse

//递归获得角色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)&#63;$roleObjArr:array();
}

How to use recursive functions in php

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 loops to reverse string 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

Recursive functions can often be replaced by loops. It is recommended to use them when we cannot replace them with loops, 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 especially suitable for browsing dynamic data structures, such as trees and lists.
Few web applications require the use of complex data structures

<&#63;php
function reversr_r($str)
{
if (strlen($str)>0)
reverse_r(substr($str,1));
echo substr($str,0,1);
return;
}
&#63;>

<&#63;php
function reverse_i($str)
{
for($i=1;$i<=strlen($str);$i++)
{
echo substr($str,-$i,1);
}
}

This program listing implements two functions, both of which can print the contents of a string in reverse order
Function reversr_r is implemented through recursion, while function reverse_i() is implemented through loop

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825220.htmlTechArticle//Recursively obtain the role ID string function explodeRole($roleObj, lt; count($roleObj-childRoleObjArr)) { foreach($roleObj-childRoleObjArr as $childRoleObj){ if('' == $resultStr){ $resul...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn