PHP generates a classification tree after all categories are removed through the interface, in the case of infinite class classification
Application Scenarios:
Through the web interface to get to the infinite classification of the Department of Data, Department of data parsing out of the format of a two-dimensional array. Because it is the temporary write a simple small frame, did not join the cache mechanism, so can only be the department to display all the page, recursive implementation, but the complexity is too high.
Workaround:
The premise is to use recursive methods, but to generate a new three-dimensional array of data obtained through the interface, and then perform recursive operations, the complexity is relatively low. (If there is a more simple way for the great God, please answer, humbly.) )
Implementation method:
1, processing the data obtained through the interface.
/* * Data processed into a three-dimensional array * @param $dept array json parsed arrays * @return Array * */function Dept_enarray ($dept = Array ()) { $de Pt_array = Array (); foreach ($dept as $key = + $val) { if ($val [' Parentno ']) { $dept _array[$val [' parentno ']][0][] = $val; } else{ $dept _array[0][] = $val; } } return $dept _array;}
2. Recursively manipulate the resulting three-dimensional array and generate HTML.
/* * Build Organizational Department tree * @param $orgdept array dept_enarray function processed arrays * @param $html String HTML tag contents * @param $LV int current operation Level * @param $parentarr Array has a department code for the Child department * @param $orgarr array of original incoming arrays * return string HTML * */function Get_depttree ($ orgdept, $html = ', $lv =1, $parentarr = ', $orgarr = ') {$VL = $LV; if (! $orgarr) $orgarr = $orgdept; if (! $parentarr && $LV = = 1) $parentarr = Array_keys ($orgdept); foreach ($orgdept [0] as $key = + $val) {if (!in_array ($val [' Deptno '], $parentarr)) $disvisiable = ' class= ' Disvisiable "'; else $disvisiable = '; if ($lv = = 1) {$html. = '
-
-
'. $val [' name ']. '
- Add a child department
- Add Member
- Editorial department
- Delete Department
'; }else{$html. = '
-
-
'. $val [' name ']. '
- Add a child department
- Add Member
- Editorial department
- Delete Department
'; } if (In_array ($val [' Deptno '], $parentarr)) {$html = Get_depttree ($orgarr [$val [' deptno]], $html, + + $VL, $pa Rentarr, $orgarr); } if ($lv = = 1) $html. = '
'; } return $html;}
The HTML effect of the last implementation is as follows.
PS: If there is a better solution to the great God, please leave a message, humbly.