Multiple instances of php array recursive method

WBOY
Release: 2016-07-25 09:11:57
Original
1121 people have browsed it

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. }

复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!