Infinite classification is a common situation in development, so this paper summarizes the common algorithm of infinite Pole classification.
1. Loop Iteration Implementation
1 $arr = [2 1=>[' id ' =>1, ' name ' = = ' parent 1 ', ' father ' =>null], 3 2=>[' id ' =>2, ' name ' = = ' parent 2 ', ' father ' =& Gt NULL], 4 3=>[' id ' =>3, ' name ' = = ' parent 3 ', ' father ' =>null], 5 4=>[' id ' =>4, ' name ' = = ' son 1-1 ', ' father ' = 1], 6 5=>[' id ' =>5, ' name ' = ' 1-2 ', ' father ' =>1], 7 6=>[' id ' =>6, ' name ' = = ' son 1-3 ', ' father ' =>1], 8 7=>[' id ' =>7, ' name ' = ' 2-1 ', ' father ' =>2], 9 8=>[' id ' =>8, ' name ' = = ' son 2-1 ', ' father ' =>2],10 9=>[' id ' =>9, ' name ' = ' 3-1 ', ' father ' =>3],11 10=>[' id ' =>10, ' name ' = ' son 3-1-1 ', ' father ' =>9], 11=>[' id ' =>11, ' name ' = ' 1-1-1 ', ' father ' =>4],13 12=>[' id ' =>12, ' name ' = ' son 2-1-1 ', ' father ' =& gt;7],14];15 function Generatetree ($items) {$tree = Array (), and ($items as $item) {if (Isset ($it ems[$item [' father ']]) {$items [$item [' father ']][' son '] = & $items [$item [' id ']];}else{21 $tree [] = & $items[$item [' id ']];22}23}24 return $tree;}26 $tree = Generatetree ($arr); Print_r (Json_encode ($tree));
Output:
Analysis:
This algorithm uses the loop iteration to output the linear structure in the tree structure according to the parent-child relationship, and the key of the algorithm is to use the reference.
Advantages: fast speed and high efficiency.
Disadvantage: The key value of the array must be the same as the ID value, and it is not easy to fetch the data (using recursion to get the data)
2. Recursive implementations
$arr = [0=>[' id ' =>1, ' name ' = = ' parent 1 ', ' father ' =>0], 1=>[' id ' =>2, ' name ' = = ' parent 2 ', ' father ' =>0], 2 = >[' id ' =>3, ' name ' = = ' parent 3 ', ' father ' =>0], 3=>[' id ' =>4, ' name ' = ' 1-1 ', ' father ' =>1], 4=>[' ID ' =>5, ' name ' = ' son 1-2 ', ' father ' =>1], 5=>[' id ' =>6, ' name ' = ' son 1-3 ', ' father ' =>1], 6=>[' id ' = 7, ' name ' = ' son 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 ' = ' son 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 ']. ' <br> ';}
Output:
Analysis:
Using recursion, the key value of the array can be different from the ID value, and finally the array is output in sequential structure
Pros: Easy traversal, find parent-child elements
Cons: PHP is not good at recursion, the volume of data is large, the efficiency will be significantly reduced
PHP iterative and recursive implementation of infinite class classification