Home  >  Article  >  Backend Development  >  Detailed explanation of infinite level classification using PHP (iteration + recursion)

Detailed explanation of infinite level classification using PHP (iteration + recursion)

墨辰丷
墨辰丷Original
2018-05-18 11:33:301782browse

This article mainly introduces PHP iteration and recursion to achieve infinite classification in detail, which has certain reference value. Interested friends can refer to it

Infinite classification is common in development situation, so this article summarizes the common infinite pole classification algorithms.

1. Loop iteration implementation

$arr = [
  1=>['id'=>1,'name'=>'父1','father'=>NULL],
  2=>['id'=>2,'name'=>'父2','father'=>NULL],
  3=>['id'=>3,'name'=>'父3','father'=>NULL],
  4=>['id'=>4,'name'=>'儿1-1','father'=>1],
  5=>['id'=>5,'name'=>'儿1-2','father'=>1],
  6=>['id'=>6,'name'=>'儿1-3','father'=>1],
  7=>['id'=>7,'name'=>'儿2-1','father'=>2],
  8=>['id'=>8,'name'=>'儿2-1','father'=>2],
  9=>['id'=>9,'name'=>'儿3-1','father'=>3],
  10=>['id'=>10,'name'=>'儿3-1-1','father'=>9],
  11=>['id'=>11,'name'=>'儿1-1-1','father'=>4],
  12=>['id'=>12,'name'=>'儿2-1-1','father'=>7],
];
function generateTree($items){
  $tree = array();
  foreach($items as $item){
    if(isset($items[$item['father']])){
      $items[$item['father']]['son'][] = &$items[$item['id']]; 
    }else{
      $tree[] = &$items[$item['id']];
    }
  }
  return $tree;
}
$tree = generateTree($arr);
print_r(json_encode($tree));

Output:

Analysis:

This algorithm uses loop iteration to output the linear structure in a tree structure according to the parent-child relationship. The key to the algorithm is the use of references.

Advantages: fast, high efficiency.

Disadvantages: the key value of the array must be the same as the id value, which is inconvenient to retrieve data (also use iteration to obtain data)

2. Recursive implementation

$arr = [
  0=>['id'=>1,'name'=>'父1','father'=>0],
  1=>['id'=>2,'name'=>'父2','father'=>0],
  2=>['id'=>3,'name'=>'父3','father'=>0],
  3=>['id'=>4,'name'=>'儿1-1','father'=>1],
  4=>['id'=>5,'name'=>'儿1-2','father'=>1],
  5=>['id'=>6,'name'=>'儿1-3','father'=>1],
  6=>['id'=>7,'name'=>'儿2-1','father'=>2],
  7=>['id'=>8,'name'=>'儿2-1','father'=>2],
  8=>['id'=>9,'name'=>'儿3-1','father'=>3],
  9=>['id'=>10,'name'=>'儿3-1-1','father'=>9],
  10=>['id'=>11,'name'=>'儿1-1-1','father'=>4],
  11=>['id'=>12,'name'=>'儿2-1-1','father'=>7],
];
function generateTree($arr,$id,$step){
  static $tree=[];
  foreach($arr as $key=>$val) {
    if($val['father'] == $id) {
      $flg = str_repeat('└―',$step);
      $val['name'] = $flg.$val['name'];
      $tree[] = $val;
      generateTree($arr , $val['id'] ,$step+1);
    }
  }
  return $tree;
}
$tree = generateTree($arr,0,0);
foreach ($tree as $val){
  echo $val[&#39;name&#39;].&#39;<br>&#39;;
}

Output:

Analysis:

uses recursion, array The key value and the id value can be different, and finally the array is output in a sequential structure

Advantages: Convenient to traverse, find parent and child elements

Disadvantage: PHP is not good at recursion, efficiency when the amount of data is large Will significantly reduce

Related recommendations:

PHP method to implement Infinitus classification tree

PHP Infinitus Classification

Detailed explanation of PHP Infinitus classification examples

##

The above is the detailed content of Detailed explanation of infinite level classification using PHP (iteration + recursion). For more information, please follow other related articles on the PHP Chinese website!

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