Welcome to the Linux community forum and interact with 2 million technical staff. The above tree generation method can also be simplified to five lines: code example :? PhpfunctiongenerateTree ($ items) {foreach ($ itemsas $ item) $ items [$ item [pid] [son] [$ item [id] $ items [$ item [id]; returnisset
Welcome to the Linux community forum and interact with 2 million technical staff> go to the above tree generation method and streamline it to five lines: Sample Code :? Php function generateTree ($ items) {foreach ($ items as $ item) $ items [$ item ['pid '] ['son'] [$ item ['id'] = $ items [$ item ['id']; return isset
Welcome to the Linux community forum and interact with 2 million technicians>
The preceding tree generation method can also be reduced to five rows:
Sample Code:
Function generateTree ($ items ){
Foreach ($ items as $ item)
$ Items [$ item ['pid '] ['son'] [$ item ['id'] = & $ items [$ item ['id'];
Return isset ($ items [0] ['son'])? $ Items [0] ['son']: array ();
}
The above method of tree structure of infinitus classified data is worth learning.
It seems that the actual use of this Code is not obvious. to retrieve the formatted tree data, we still need to recursion:
Sample Code:
/**
* How to retrieve formatted tree data
* @ Site <
*/
$ Tree = generateTree ($ items );
Function getTreeData ($ tree ){
Foreach ($ tree as $ t ){
Echo $ t ['name'].'
';
If (isset ($ t ['son']) {
GetTreeData ($ t ['son']);
}
}
}
GetTreeData ($ tree );
[1] [2]