The discussion on recursive algorithms is not yet graduated. now I am working on a project and have encountered recursive problems. I hope you can help me!
The problem is very simple. the problem of classical recursive traversal of arrays
Array ('id' => '1', 'name' => 'role management', 'pid '=> '0 ',), '1' => array ('id' => '2', 'name' => 'Add role ', 'pid' => '1 ',), '2' => array ('id' => '3', 'name' => 'role list', 'pid '=> '2 ',), '3' => array ('id' => '4', 'name' => 'menu management', 'pid '=> '0 ',), '4' => array ('id' => '5', 'name' => 'foreground management', 'pid '=> '4 ',), '5' => array ('id' => '6', 'name' => 'Add', 'pid '=> '5 ',), '6' => array ('id' => '7', 'name' => 'modify', 'pid '=> '5 ',), '7' => array ('id' => '8', 'name' => 'list', 'pid '=> '5 ',), '8' => array ( 'Id' => '9', 'name' => 'background management', 'pid '=> '4 ',), '9' => array ('id' => '10', 'name' => 'Add', 'pid '=> '9 ',), '10' => array ('id' => '11', 'name' => 'modify', 'pid '=> '9 ',), '11' => array ('id' => '12', 'name' => 'list', 'pid '=> '9',),);?>
In this way, recursive traversal requires two results.
An array of standard hierarchical relationships
Another one is used to put it in the drop-down list,
In addition, I checked the information and found that pointers could be used. students are still very simple. please give me some advice !!!
Reply to discussion (solution)
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 ')*) */public function _ construc T ($ arr = array () {$ this-> arr = $ arr; $ this-> ret = ''; return is_array ($ arr );} /*** get the parent array * @ param int * @ return array */public 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 */public function get_child ($ myid) {$ a = $ newarr = array (); if (is_array ($ this-> arr) {foreach ($ this-> arr as $ id => $ a) {if ($ a ['parent Id'] = $ myid) $ newarr [$ id] = $ a ;}} return $ newarr? $ Newarr: false;}/*** get the current position array * @ param int * @ return array */public 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 $ a ;}/ *** get the tree structure * @ param int id, obtain all sub-levels under this ID * @ param string to generate the basic code of the tree structure, for example :"\ $ Spacer \ $ name"* @ Param int indicates the selected ID. for example, you need to use * @ return string */public function get_tree ($ myid, $ str, $ sid = 0, $ adds = '', $ str_group ='') {$ number = 1; $ child = $ this-> get_child ($ myid); print_r ($ child ); if (is_array ($ child) {$ total = count ($ child); foreach ($ child as $ id => $ value) {$ j = $ k = ''; if ($ number = $ total) {$ j. = $ this-> icon [2]; // if it is the last child, use the cursor} else {$ j. = $ this-> icon [1]; $ k = $ adds? $ This-> icon [0]: '';} $ spacer = $ adds? $ Adds. $ j: ''; // attributes $ selected = $ id = $ sid? 'Selected': ''; // select @ extract ($ value) from the drop-down list. // this method assigns the corresponding key value $ nstr to the following character variables, $ str_group $ parentid = 0 & $ str_group? Eval ("\ $ nstr = \" $ str_group \ ";"): eval ("\ $ nstr = \" $ str \ ";"); $ this-> ret. = $ nstr; $ nbsp = $ this-> nbsp; $ this-> get_tree ($ id, $ str, $ sid, $ adds. $ k. $ nbsp, $ str_group); // if the subset is an array, call $ number ++ recursively.} return $ this-> ret ;}} $ arr = array (1 => array ('id' => '1', 'parentid' => 0, 'name' => 'world '), 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'China '), 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'u.s '), 5 => array ('id' => '5', 'parentid' => 3, 'name' => 'hunan '), 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Beijing '), 7 => array ('id' => '7', 'parentid' => 4, 'name' => 'Newyork '), 7 => array ('id' => '7', 'parentid' => 5, 'name' => 'Changsha ')); $ tree = new tree ($ arr); $ str ="
"; Echo $ tree-> get_tree (0, $ str);?>
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 ')*) */public function _ construc T ($ arr = array () {$ this-> arr = $ arr; $ this-> ret = ''; return is_array ($ arr );} /*** get the parent array * @ param int * @ return array */public 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 */public function get_child ($ myid) {$ a = $ newarr = array (); if (is_array ($ this-> arr) {foreach ($ this-> arr as $ id => $ a) {if ($ a ['parent Id'] = $ myid) $ newarr [$ id] = $ a ;}} return $ newarr? $ Newarr: false;}/*** get the current position array * @ param int * @ return array */public 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 $ a ;}/ *** get the tree structure * @ param int id, obtain all sub-levels under this ID * @ param string to generate the basic code of the tree structure, for example :"\ $ Spacer \ $ name"* @ Param int indicates the selected ID. for example, you need to use * @ return string */public function get_tree ($ myid, $ str, $ sid = 0, $ adds = '', $ str_group ='') {$ number = 1; $ child = $ this-> get_child ($ myid); print_r ($ child ); if (is_array ($ child) {$ total = count ($ child); foreach ($ child as $ id => $ value) {$ j = $ k = ''; if ($ number = $ total) {$ j. = $ this-> icon [2]; // if it is the last child, use the cursor} else {$ j. = $ this-> icon [1]; $ k = $ adds? $ This-> icon [0]: '';} $ spacer = $ adds? $ Adds. $ j: ''; // attributes $ selected = $ id = $ sid? 'Selected': ''; // select @ extract ($ value) from the drop-down list. // this method assigns the corresponding key value $ nstr to the following character variables, $ str_group $ parentid = 0 & $ str_group? Eval ("\ $ nstr = \" $ str_group \ ";"): eval ("\ $ nstr = \" $ str \ ";"); $ this-> ret. = $ nstr; $ nbsp = $ this-> nbsp; $ this-> get_tree ($ id, $ str, $ sid, $ adds. $ k. $ nbsp, $ str_group); // if the subset is an array, call $ number ++ recursively.} return $ this-> ret ;}} $ arr = array (1 => array ('id' => '1', 'parentid' => 0, 'name' => 'world '), 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'China '), 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'u.s '), 5 => array ('id' => '5', 'parentid' => 3, 'name' => 'hunan '), 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Beijing '), 7 => array ('id' => '7', 'parentid' => 4, 'name' => 'Newyork '), 7 => array ('id' => '7', 'parentid' => 5, 'name' => 'Changsha ')); $ tree = new tree ($ arr); $ str ="
"; Echo $ tree-> get_tree (0, $ str);?>
The returned result is a string. how can I return an array?
There are too many posts in the essence area. I suggest you take a look at them first.
Innovation is generally performed only when you cannot find or are dissatisfied with others' experiences.
There are too many posts in the essence area. I suggest you take a look at them first.
Innovation is generally performed only when you cannot find or are dissatisfied with others' experiences.
Thank you!