tree structure, Multi-dimensional Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
st3fanos
Forum Newbie
Posts: 12
Joined: Tue Apr 13, 2010 1:30 am

tree structure, Multi-dimensional Array

Post by st3fanos »

Hi,

I'v got a complete mind block and am looking for a pointer.

I am using the 'Nested Set Model Data' to store a directory tree structure in my database.

I have got as far as being able to put it into an assosiative array Name and Depth of each node .

Code: Select all

Name -- Portable Electronics
depth -- 1
Name -- Mp3 Players
depth -- 2
Name -- Flash
depth -- 3
Name -- CD Player
depth -- 2
and also have been able to put this into a Multi-dimensional Arrays.

Code: Select all

    array(5) {
      ["Name"]=>
      string(20) "Portable Electronics"
      ["depth"]=>
      string(1) "1"
      [7]=>
      array(3) {
        ["Name"]=>
        string(11) "Mp3 Players"
        ["depth"]=>
        string(1) "2"
        [8]=>
        array(2) {
          ["Name"]=>
          string(5) "Flash"
          ["depth"]=>
          string(1) "3"
        }
      }
      [9]=>
      array(2) {
        ["Name"]=>
        string(9) "CD Player"
        ["depth"]=>
        string(1) "2"
      }
      [10]=>
      array(2) {
        ["Name"]=>
        string(5) "Radio"
        ["depth"]=>
        string(1) "2"
      }
    }
I would like to convert this into a simple text output like:

Code: Select all

	var portable_electronics = new WebFXTreeItem('Portable Electronics');
	var mp3_players = new WebFXTreeItem('Mp3 Players');
	var cd_player = new WebFXTreeItem('CD Player');
	var radio = new WebFXTreeItem('Radio');
	var flash = new WebFXTreeItem('Flash');

	portable_electronics.add(mp3_players);
	mp3_players.add(flash);
	portable_electronics.add(cd_player);
	portable_electronics.add(radio);
The bit I just cannot work out is how do you find the parent node of a Multi-dimensional Array so I can create the a line like: portable_electronics.add(mp3_players);

Kind regards
Stephen
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: tree structure, Multi-dimensional Array

Post by requinix »

The course of action might be a bit more obvious if you change the (JavaScript, I assume) output to be

Code: Select all

var portable_electronics = new WebFXTreeItem('Portable Electronics');
var mp3_players = new WebFXTreeItem('Mp3 Players');
portable_electronics.add(mp3_players);
var flash = new WebFXTreeItem('Flash');
mp3_players.add(flash);

var radio = new WebFXTreeItem('Radio');
portable_electronics.add(radio);

var cd_player = new WebFXTreeItem('CD Player');
portable_electronics.add(cd_player);
Use a recursive function. Some code to help:

Code: Select all

$name = "Portable Electronics";
$var = strtolower(trim("-", preg_replace('/[^a-z0-9_]+/i', '-', $name)));
$quoted = addslashes($name);

echo "var {$var} = new WebFXTreeItem(\"{$quoted}\");\n";
st3fanos
Forum Newbie
Posts: 12
Joined: Tue Apr 13, 2010 1:30 am

Re: tree structure, Multi-dimensional Array

Post by st3fanos »

Hi,

As much as I try I am bound by the dimentions of my programming world, like the circle in flatland. "Multi-dimensional Array" and object are inconceivable, I need to be shown the world from this new dimention.

As for my problem I have finally worked this out, I am not sure it's the best way.
It works like this scan through every node in the tree and get it's sub-nodes, a flatland way of thinking :D

Code: Select all

$iterator = new RecursiveIteratorIterator(new recursiveArrayIterator($hierachy->fullTree('Electronics')));
try {
	//Get every tree item
    foreach($iterator as $key=>$value)
        {
			//Then get the child nodes, 
			$iterator2 = new RecursiveIteratorIterator(new recursiveArrayIterator($hierachy->getLocalSubNodes($value,0)));
			try {
				foreach($iterator2 as $key2=>$value2)
					{
						if($key2 != 'depth'){
							echo $value.' -- '.$value2.'<br />';
						}
					}
			}
			catch(Exception $e)
				{
				echo $e->getMessage();
			}
        }
}
catch(Exception $e)
    {
    echo $e->getMessage();
}
Is there a better way ?

Kind Regards
Stephen
Post Reply