Jump to content

recursive and multidimensional array


Akira

Recommended Posts

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.