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

墨辰丷
Release: 2023-03-26 14:42:01
Original
1736 people have browsed it

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));
Copy after login

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['name'].'
'; }
Copy after login

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:

##

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!

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
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!