PHP recursively traverses multi-dimensional arrays to achieve infinite classification, recursive multi-dimensional_PHP tutorial

WBOY
Release: 2016-07-12 08:53:39
Original
721 people have browsed it

The method of PHP recursively traversing multi-dimensional arrays to achieve infinite classification, recursive multi-dimensional

The example in this article describes the method of PHP recursively traversing multi-dimensional arrays to achieve infinite classification. Share it with everyone for your reference, the details are as follows:

<&#63;php
   //$data[]=array('id'=>1,'parentid'=>0,'name'=>'中国','img'=>'52091199');
   $data[]=array('id'=>1,'parentid'=>0,'name'=>'中国');
   $data[]=array('id'=>2,'parentid'=>0,'name'=>'美国');
   $data[]=array('id'=>3,'parentid'=>0,'name'=>'韩国');
   $data[]=array('id'=>4,'parentid'=>1,'name'=>'北京');
   $data[]=array('id'=>5,'parentid'=>1,'name'=>'上海');
   $data[]=array('id'=>6,'parentid'=>1,'name'=>'广西');
   $data[]=array('id'=>7,'parentid'=>6,'name'=>'桂林');
   $data[]=array('id'=>8,'parentid'=>6,'name'=>'南宁');
   $data[]=array('id'=>9,'parentid'=>6,'name'=>'柳州');
   $data[]=array('id'=>10,'parentid'=>2,'name'=>'纽约');
   $data[]=array('id'=>11,'parentid'=>2,'name'=>'华盛顿');
   $data[]=array('id'=>12,'parentid'=>3,'name'=>'首尔');
   $tree=build_tree($data,0);
   //echo memory_get_usage();
   print_r($tree);
 function findChild(&$arr,$id){
  $childs=array();
  foreach ($arr as $k => $v){
   if($v['parentid']== $id){
    $childs[]=$v;
   }
  }
  return $childs;
 }
 function build_tree($rows,$root_id){
  $childs=findChild($rows,$root_id);
  if(empty($childs)){
   return null;
  }
 foreach ($childs as $k => $v){
  $rescurTree=build_tree($rows,$v['id']);
  if( null != $rescurTree){
  $childs[$k]['childs']=$rescurTree;
  }
 }
  return $childs;
 }
&#63;>

Copy after login

Run result:

Array
(
 [0] => Array
  (
   [id] => 1
   [parentid] => 0
   [name] => 中国
   [childs] => Array
    (
     [0] => Array
      (
       [id] => 4
       [parentid] => 1
       [name] => 北京
      )
     [1] => Array
      (
       [id] => 5
       [parentid] => 1
       [name] => 上海
      )
     [2] => Array
      (
       [id] => 6
       [parentid] => 1
       [name] => 广西
       [childs] => Array
        (
         [0] => Array
          (
           [id] => 7
           [parentid] => 6
           [name] => 桂林
          )
         [1] => Array
          (
           [id] => 8
           [parentid] => 6
           [name] => 南宁
          )
         [2] => Array
          (
           [id] => 9
           [parentid] => 6
           [name] => 柳州
          )
        )
      )
    )
  )
 [1] => Array
  (
   [id] => 2
   [parentid] => 0
   [name] => 美国
   [childs] => Array
    (
     [0] => Array
      (
       [id] => 10
       [parentid] => 2
       [name] => 纽约
      )
     [1] => Array
      (
       [id] => 11
       [parentid] => 2
       [name] => 华盛顿
      )
    )
  )
 [2] => Array
  (
   [id] => 3
   [parentid] => 0
   [name] => 韩国
   [childs] => Array
    (
     [0] => Array
      (
       [id] => 12
       [parentid] => 3
       [name] => 首尔
      )
    )
  )
)

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1123795.htmlTechArticleHow to recursively traverse multi-dimensional arrays in PHP to achieve infinite classification, recursive multi-dimensional This article describes the example of PHP recursively traversing multi-dimensional arrays to achieve infinite Classification method. Share it with everyone for your reference, with...
Related labels:
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