A relatively complete tree structure code
Array ('id' => '1', 'parentid' => 0, 'name' => 'Level 1 topic 1 '), * 2 => array ('id' => '2', 'parentid' => 0, 'name' => 'Level 1 column 2 '), * 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level column 1 '), * 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '), * 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '), * 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Level 3 topic 1 '), * 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-level column 2 ')*) */function tree ($ arr = array ()) {$ This-> arr = $ arr; $ this-> ret = ''; return is_array ($ arr );} /*** get the parent array * @ param int * @ return array */function get_parent ($ myid) {$ newarr = array (); if (! Isset ($ this-> arr [$ myid]) return false; $ pid = $ this-> arr [$ myid] ['parentid']; $ pid = $ this-> arr [$ pid] ['parentid']; if (is_array ($ this-> arr )) {foreach ($ this-> arr as $ id => $ a) {if ($ a ['parentid'] = $ pid) $ newarr [$ id] = $ a;} return $ newarr ;} /*** get the sub-level array * @ param int * @ return array */function get_child ($ myid) {$ a = $ newarr = array (); if (is_array ($ this-> arr) {foreach ($ this-> arr as $ id => $ a) {if ($ a ['parentid'] = $ Myid) $ newarr [$ id] = $ a ;}} return $ newarr? $ Newarr: false;}/*** get the current position array * @ param int * @ return array */function get_pos ($ myid, & $ newarr) {$ a = array (); if (! Isset ($ this-> arr [$ myid]) return false; $ newarr [] = $ this-> arr [$ myid]; $ pid = $ this-> arr [$ myid] ['parentid']; if (isset ($ this-> arr [$ pid]) {$ this-> get_pos ($ pid, $ newarr);} if (is_array ($ newarr) {krsort ($ newarr); foreach ($ newarr as $ v) {$ a [$ v ['id'] = $ v ;}} return $ ;} /*** --------------------------------------- * get the tree structure ** --------------------------------------- * @ author Midnight (Yang Yunzhou ), yangyunzhou@foxmail.com * @ param $ myid indicates to get all sub-levels under this ID * @ param $ str generate the basic code of the tree structure, for example :"$ Spacer $ name"* @ Param $ ID of the selected sid, for example, you need to use * @ param $ adds * @ param $ str_group * @ return unknown_type */function get_tree ($ myid, $ str, $ sid = 0, $ adds = '', $ str_group ='') {$ number = 1; $ child = $ this-> get_child ($ myid); if (is_array ($ child )) {$ total = count ($ child); foreach ($ child as $ id => $ a) {$ j = $ k = ''; if ($ number = $ total) {$ j. = $ this-> icon [2];} else {$ j. = $ this-> icon [1]; $ k = $ adds? $ This-> icon [0]: '';} $ spacer = $ adds? $ Adds. $ j: ''; $ selected = $ id = $ sid? 'Selected': ''; @ extract ($ a); $ parentid = 0 & $ str_group? Eval ("$ nstr =" $ str_group ";"): eval ("$ nstr =" $ str ";"); $ this-> ret. = $ nstr; $ this-> get_tree ($ id, $ str, $ sid, $ adds. $ k. '', $ str_group); $ number ++;} return $ this-> ret;}/*** similar to the previous method, however, you can select */function get_tree_multi ($ myid, $ str, $ sid = 0, $ adds = '') {$ number = 1; $ child = $ this-> get_child ($ myid); if (is_array ($ child) {$ total = count ($ child ); foreach ($ child as $ id => $ a) {$ j = $ k = ''; if ($ number = $ total) {$ j. = $ this-> Icon [2];} else {$ j. = $ this-> icon [1]; $ k = $ adds? $ This-> icon [0]: '';} $ spacer = $ adds? $ Adds. $ j: ''; $ selected = $ this-> have ($ sid, $ id )? 'Selected': ''; // echo $ sid. '=>'. $ id. ':'. $ selected .'.
'; @ Extract ($ a); eval ("$ nstr =" $ str ";"); $ this-> ret. = $ nstr; $ this-> get_tree_multi ($ id, $ str, $ sid, $ adds. $ k. ''); $ number ++;} return $ this-> ret;} function have ($ list, $ item) {return (strpos (',,'. $ list. ',',','. $ item. ',') ;}}?>