>  기사  >  백엔드 개발  >  php数组递归方法多个实例

php数组递归方法多个实例

WBOY
WBOY원래의
2016-07-25 09:11:571121검색

php数组递归方法

有如下php数组:

  1. function genTree5($items) {
  2. foreach ($items as $item)
  3. $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
  4. return isset($items[0]['son']) ? $items[0]['son'] : array();
  5. }
复制代码

方法二:

  1. function findChild($arr,$id){

  2. $childs=array();
  3. foreach ($arr as $k => $v){
  4. if($v['pid']== $id){
  5. $childs[]=$v;
  6. }
  7. }
  8. // echo "
    ";print_r($childs);die(); 
  9. return $childs;
  10. }
  11. function build_tree($root_id){

  12. global $items;
  13. $childs =array();
  14. $childs=findChild($items,$root_id);
  15. // print_r($childs);
  16. // die();
  17. if(empty($childs)){
  18. return null;
  19. }
  20. foreach ($childs as $k => $v){
  21. $rescurTree=build_tree($v['id']);
  22. if( null != $rescurTree){
  23. $childs[$k]['son']=$rescurTree;
  24. }
  25. }
  26. return $childs;
  27. }
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.