Problem: Design the company's staff table, employees have different levels, the requirements can be found through an employee to all employees of their subordinates, but also to their own superiors.
Solution: Recursive implementation of infinite classification ideas, each employee to the parent's ID as their own PID, in addition to Path,path contains its own ID, convenient display path.
Note Points for recursion:
1. There must be a judgment condition, otherwise the recursion will die cycle
2. Each recursive result to be saved, variable self-increment implementation of three ways:
Function (& $bar): By reference address
Global: By setting to a Globals variable
Static: set to static variable
Header("content-type:text/html; Charset=utf-8 " );$arr=Array( Array(' uid ' = ' 1 ', ' name ' = ' director Zhang ', ' pid ' = ' 0 ', ' path ' = ' 0, '),Array(' uid ' = ' 2 ', ' name ' = ' VP ', ' pid ' = ' 1 ', ' path ' = ' 0, 1, '),Array(' uid ' = ' 3 ', ' name ' = ' manager Zhao ', ' pid ' = ' 2 ', ' path ' = ' = ' 0,1,2, '),Array(' uid ' = ' 4 ', ' name ' = ' Employee Zhao ', ' pid ' = ' 6 ', ' path ' = ' = ' 0,1,2,3,6, '),Array(' uid ' = ' 5 ', ' name ' = ' employee room ', ' pid ' = ' 6 ', ' path ' = ' = ' 0,1,2,3,6, '),Array(' uid ' = ' 6 ', ' name ' = ' leader Liu ', ' pid ' = ' 3 ', ' path ' = ' = ' 0,1,2,3, '),Array(' uid ' = ' 7 ', ' name ' = ' manager light rod ', ' pid ' = ' 2 ', ' path ' = ' 0,1,2, '),);/** * by passing in the PID of the person you are looking for to find his subordinates, the code is rudimentary, the spirit of understanding is the main * @param array $data arrays instead of data in the database * @param integer $pid Parent ID * @param Array & $result result arrays,& guaranteed variable resident * @param integer $deep output delimiter--, meaningless * @return tree-like array*/functionGetList ($data,$pid=0, &$result=Array(),$deep= 0 ) { $deep+=2; foreach($data as $key=$val ) { if($pid==$val[' PID '] ) { $result[] = "|".str_repeat("--",$deep).$val[' Name ']; GetList ($data,$val[' UID '],$result,$deep ); } } return $result;}//Call function Output result$res= GetList ($arr, 1); //Find the subordinate of Vice King foreach($res as $key=$val){ Echo $val." \ n ";}
/*
|----Vice King
|--------Manager Zhao
|------------leader Liu
|----------------Staff Zhao
|----------------Staff Room
|--------Manager Light Pole
*/
Or the above data, now look for an employee of all the superiors
/** * The ID of the incoming employee can find the parent to whom the employee belongs * @param array $data array instead of data in the database * @param integer $id ID of the employee to find * @param array & $result result array,& guaranteed variable resident * @return result*/functionGetLink ($data,$id=0, &$result=Array()){ foreach($data as $key=$val) { if($val[' uid '] = =$id) { $result[] =$val[' Name ']; GetLink ($data,$val[' PID '],$result); } } return $result;}$res= GetLink ($arr, 5);//find the superior of the employee's roomforeach($res as $key=$val) { Echo $val." | ";}/*Staff Room | leader Liu | Manager Zhao | Vice President | Director Zhang | */
Note: The full path can also be classified as infinite: The path is stored in the data, and the full route is calculated by concat (path, ",", id).
Advantages: Fast query, faster than recursion
Cons: increased, slightly more complex when moving categories
Recursive finding of a tree structure in infinite pole classification