Does PHP implement hierarchical tree display of data recursively? & Lt ;? Php $ dbmysql_connect (localhost, root, root) ordie (Cantconnecttodatabase); mysql_select_db (test) ordie (Cantfinddatabase: tes PHP recursive hierarchical tree display of data
?
$ Arr ['id'], 'fid' => $ arr ['fid'], 'name' => $ arr ['name'],);} // sort data by indentation. for details, see function data2arr ($ tree, $ rootId = 0, $ level = 0) {foreach ($ tree as $ leaf) {if ($ leaf ['fid'] = $ rootId) {echo str_repeat ('', $ level ). $ leaf ['id']. ''. $ leaf ['name'].'
'; Foreach ($ tree as $ l) {if ($ l ['fid'] = $ leaf ['id']) {data2arr ($ tree, $ leaf ['id'], $ level + 1); break ;}}} data2arr ($ data); echo'
-----------------------------------
'; // Encapsulate data according to the link. for details, see function arr2tree ($ tree, $ rootId = 0) {$ return = array (); foreach ($ tree as $ leaf) {if ($ leaf ['fid'] = $ rootId) {foreach ($ tree as $ subleaf) {if ($ subleaf ['fid'] = $ leaf ['id']) {$ leaf ['Children '] = arr2tree ($ tree, $ leaf ['id']); break ;}$ return [] = $ leaf ;}return $ return ;}$ tree = arr2tree ($ data ); print_r ($ tree); echo'
-----------------------------------
'; // Use HTML to display data again. for details, see function tree2html ($ tree) {echo'
'; Foreach ($ tree as $ leaf) {echo'
- '. $ Leaf ['name']; if (! Empty ($ leaf ['Children ']) tree2html ($ leaf ['Children']); echo'
';} Echo'
';} Tree2html ($ tree );
?
?
?
Figure 1:
?Figure 2:
??Figure 3: