Tag:php tree menu
<?phpheader ("Content-type:text/html; charset=utf-8"); $mixTree = array ();/* * Key data in each result two one is the ID of the parent */that points to its parent node ID $mixTree [] = array (' id ' =>0, ' val ' = ' top menu 1 ', ' Parent ' =>-1); $mixTree [] = array (' id ' =>1, ' val ' = ' top menu 2 ', ' parent ' =>-1); $mixTree [] = array (' id ' =>2, ' val ' = ' + ' top menu 1 under Level Two menu 1 ', ' parent ' =>0); $mixTree [] = array (' id ' =>3, ' val ' = > ' top Menu 1 under Level Two menu 2 ', ' parent ' =>0); $mixTree [] = array (' id ' =>4, ' val ' = ' top menu 2 ' two ', ' parent ' = >1); $mixTree [] = array (' id ' =>5, ' val ' = ' + ' top menu 1 under Level Two menu 2 under three-level menu 1 ', ' parent ' =>3);/* * A two-dimensional array to mount the above data into a tree */$treeDatas = array (); foreach ($mixTree as $nodeDatas) { if ($nodeDatas [' Parent ']==-1) { $treeDatas [ -1][]=$ nodedatas; }else{ $treeDatas [$nodeDatas [ ' Parent ']][]= $nodeDatas; }}/* * Traverse prints its parent node and its child nodes */function traversesing ($nodeDatas, $treeDatas ) { echo $nodeDatas [' Val ']; if (!empty ($treeDatas [$nodeDatas [ ' ID ']]) && is_array ($treeDatas [$nodeDatas [' ID ']]) { echo ' <ul> '; foreach ($treeDatas [$nodeDatas [' ID ']] as $childNodeDatas) { echo ' <li> '; traversesing ($ childnodedatas, $treeDatas); echo ' </li> '; } echo ' </ul> '; } } echo ' <ul > '; foreach ($treeDatas [-1] as $nodeDatas) { echo ' <li> '; traversesing ($nodeDatas, $treeDatas); echo ' </li> ';} echo ' </ul> ';
Execution results
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/72/99/wKioL1Xo2OrBEQhSAADEZjp29N8554.jpg "title=" 2015-09-04_072403.png "alt=" Wkiol1xo2orbeqhsaadezjp29n8554.jpg "/>
This article is from my It career blog, so be sure to keep this source http://quietnight.blog.51cto.com/7163892/1691261
PHP Small Code----Tree menu generation