Home >php教程 >php手册 >php 递归的问题

php 递归的问题

WBOY
WBOYOriginal
2016-06-07 11:44:571411browse

php分类递归递归的问题
<?php <br /> function arrayPidProcess($data,$res=array(),$pid='0',$endlevel='1'){<br>     foreach ($data as $k => $v){<br>         if($v['pid']==$pid){<br>             $res[$v['id']]['info']=$v;<br>             if($endlevel!='0'){<br>                 if($v['level']==$endlevel){<br>                     $child=null;<br>                 }<br>                 else{<br>                     $child=arrayPidProcess($data,array(),$v['id'],$endlevel);<br>                 }<br>                 $res[$v['id']]['child']=$child;<br>             }<br>             else{<br>                 $child=arrayPidProcess($data,array(),$v['id']);<br>                 if($child==''||$child==null){<br>                     $res[$v['id']]['child']=null;<br>                 }<br>                 else{<br>                     $res[$v['id']]['child']=$child;<br>                 }<br>             }<br>             <br>         }<br>     }<br>     return $res;<br> }<br> $arr = array(<br>     array('id'=>1, 'name'=>'c', 'pid'=>0, 'level'=>1),<br>     array('id'=>2, 'name'=>'a', 'pid'=>0, 'level'=>1),<br>     array('id'=>3, 'name'=>'v', 'pid'=>1, 'level'=>2),<br>     array('id'=>4, 'name'=>'q', 'pid'=>3, 'level'=>3),<br>     array('id'=>5, 'name'=>'w', 'pid'=>4, 'level'=>4),<br>     array('id'=>6, 'name'=>'e', 'pid'=>5, 'level'=>5),<br>     array('id'=>7, 'name'=>'r', 'pid'=>0, 'level'=>1),<br>     array('id'=>8, 'name'=>'t', 'pid'=>0, 'level'=>1),<br>     array('id'=>9, 'name'=>'y', 'pid'=>0, 'level'=>1),<br>     array('id'=>10, 'name'=>'u', 'pid'=>0, 'level'=>1),<br>     array('id'=>11, 'name'=>'i', 'pid'=>0, 'level'=>1),<br> );<br> print_r(arrayPidProcess($arr));

附件 5189da8f95634.jpg!550xauto.jpg ( 453.25 KB 下载:175 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

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