Requirements Description:
A collection of existing nodes
Can be treated as a two-dimensional array
1 Array(5) {2[0] = =Array(4) {3["id"] = =string(1) "1"4["name"] + =string(5) "Admin"5["title"] = =string(12) "Background app"6["pid"] = =string(1) "0"7 }8[1] = =Array(4) {9["id"] = =string(1) "2"Ten["name"] + =string(5) "Index" One["title"] = =string(12) "Front End application" A["pid"] = =string(1) "0" - } -[2] = =Array(4) { the["id"] = =string(1) "3" -["name"] + =string(5) "Index" -["title"] = =string(12) "Backstage Home" -["pid"] = =string(1) "1" + } -[3] = =Array(4) { +["id"] = =string(1) "4" A["name"] + =string(Ten) "Msgmanager" at["title"] = =string(12) "Post management" -["pid"] = =string(1) "1" - } -[4] = =Array(4) { -["id"] = =string(1) "5" -["name"] + =string(4) "Rbac" in["title"] = =string() "Rbac permissions" -["pid"] = =string(1) "1" to } +}
Array Arr01
PID, Level field explanation:
The PID represents the ID of the parent. Value range {0,1,2,3, ..., N, ... }
The PID has a value of 0, indicating that the record does not belong to any other record.
The PID has a value of 1, which indicates that the record belongs to the record with ID 1. That is, the child record for the record with ID 1.
The PID has a value of 2, which indicates that the record belongs to the record with ID 2. That is, the child record for the record with ID 2.
......
And so on
Level represents levels and levels. The value range {x-i}, which is 3 levels, or 3 levels
The level has a value of 1, which represents the first level, the highest level, and the most high.
The level has a value of 2, representing the second layer.
The level has a value of 3, which represents the third layer.
For example, if level=1 represents the home page, company department, product introduction, and so on in the navigation bar
Level=2, then, represents department a, Department b 、... Product A, product B, product C 、...
In this way, only a two-dimensional table, you can save the next record of the attribution relationship.
However, in order to iterate through the hierarchical data in a way that uses foreach nesting, we need to combine the original two-dimensional array with the attribution relationship.
Form: a multidimensional array
1 Array(2) {2[0] = =Array(5) {3["id"] = =string(1) "1"4["name"] + =string(5) "Admin"5["title"] = =string(12) "Background app"6["pid"] = =string(1) "0"7["child"] = =Array(3) {8[0] = =Array(5) {9["id"] = =string(1) "3"Ten["name"] + =string(5) "Index" One["title"] = =string(12) "Backstage Home" A["pid"] = =string(1) "1" -["child"] = =Array(0) { - } the } -[1] = =Array(5) { -["id"] = =string(1) "4" -["name"] + =string(Ten) "Msgmanager" +["title"] = =string(12) "Post management" -["pid"] = =string(1) "1" +["child"] = =Array(0) { A } at } -[2] = =Array(5) { -["id"] = =string(1) "5" -["name"] + =string(4) "Rbac" -["title"] = =string() "Rbac permissions" -["pid"] = =string(1) "1" in["child"] = =Array(0) { - } to } + } - } the[1] = =Array(5) { *["id"] = =string(1) "2" $["name"] + =string(5) "Index"Panax Notoginseng["title"] = =string(12) "Front End application" -["pid"] = =string(1) "0" the["child"] = =Array(0) { + } A } the}
Array Arr02
let's write a recursive function Node_merge () to achieve:
1<?PHP2 /**3 * Recursive reorganization of node information multidimensional arrays4 * @param [array] $node [array of nodes to process: two-dimensional array]5 * @param [int] $pid [parent ID]6 * @return [array] [Tree-structured node system: Multidimensional arrays]7 */8 functionNode_merge ($node,$pid=0){9 $arr=Array();Ten One A foreach($node as $v) { - if($v[' PID ']==$pid) { - $v[' Child ']=node_merge ($node,$v[' ID ']); the $arr[]=$v; - } - } - + return $arr; - } +?>
How do you understand this function?
We take the data for the primitive two-dimensional array (ARR01) as an example, by calling the Node_merge () function to convert to a multidimensional array (ARR02) that can directly see the attribution relationship.
The $node in the function is Arr01, and when the Arr01 array is passed in, the $pid is initialized to a 0 value, and the function creates a new temporary array (arr);
Then, using the Foreach Loop, read the first record (id=1 that record), the judgment processing: if the first record in the parent ID is 0, then exactly equal to the initialized $pid,
Then, in the first record, add a field (child). The value of the child field in the first record, from the pid=1 record (think about why. Tip: Start with Node_merge ($node, $v [' id ']), $v[' id ' in).
And the value operation is from the $arr[]= $v;
This is using recursion, function call function, which is my understanding, if you have a better understanding, can give me a message. Email: [Email protected]
thinkphp: Using the recursive write Node_merge () function