Akira Posted June 4, 2010 Share Posted June 4, 2010 Hey freakz, Have a small issue which i just cant resolve, i want to build a recursive multidimensional array. I made a function which can read on level depts but just can't get the info in a multidimensional array. Hopefully somebody here has a anwser Function: <?php function fillRights( $id, $level) { $query = "SELECT id,nl,sub FROM tbl_rights WHERE sub = '".$id."';"; $result = mysql_query( $query ); while( $row = mysql_fetch_array( $result ) ) { echo str_repeat(' ', $level).$row['nl'].'<br />'; fillRights($row['id'],$level+1); } } fillRights( '', 0 ); ?> Table to test DROP TABLE IF EXISTS `tbl_rights`; CREATE TABLE `tbl_rights` ( `id` int(11) NOT NULL auto_increment, `nl` varchar(255) default NULL, `en` varchar(255) default NULL, `sub` varchar(11) default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1; INSERT INTO `tbl_rights` VALUES ('1', 'menu1', 'menu1', ''); INSERT INTO `tbl_rights` VALUES ('2', 'menu2', 'menu2', ''); INSERT INTO `tbl_rights` VALUES ('3', 'menu3', 'menu3', ''); INSERT INTO `tbl_rights` VALUES ('4', 'submenu 1.1', 'submenu 1.1', '1'); INSERT INTO `tbl_rights` VALUES ('5', 'submenu 2.1', 'submenu 2.1', '2'); INSERT INTO `tbl_rights` VALUES ('6', 'submenu 1.2', 'submenu 1.2', '1'); INSERT INTO `tbl_rights` VALUES ('7', 'submenu 2.2', 'submenu 2.2', '2'); INSERT INTO `tbl_rights` VALUES ('8', 'submenu.3.1', 'submenu.3.1', '3'); INSERT INTO `tbl_rights` VALUES ('9', 'menu4', 'menu4', ''); INSERT INTO `tbl_rights` VALUES ('10', 'subsubmenu 1.2.1', 'subsubmenu 1.2.1', '6'); INSERT INTO `tbl_rights` VALUES ('11', 'subsubmenu 1.2.2', 'subsubmenu 1.2.2', '6'); INSERT INTO `tbl_rights` VALUES ('12', 'subsubsubmenu 1.2.1.1', 'subsubsubmenu 1.2.1.1', '10'); My desired outcome: $rights['menu 1']['submenu 1.1']; $rights['menu 1']['submenu 1.2']; $rights['menu 1']['submenu 1.2']['subsubmenu 1.2.1']; $rights['menu 1']['submenu 1.2']['subsubmenu 1.2.1']['subsubmenu 1.2.1.1']; $rights['menu 2']; $rights['menu 2']['submenu 2.1']; $rights['menu 2']['submenu 2.2']; $rights['menu 3']; $rights['menu 3']['menu 3.1']; $rights['menu 4']; Hope some one here has a clue Quote Link to comment https://forums.phpfreaks.com/topic/203838-recursive-and-multidimensional-array/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.