I have a db table that describes a hierarchy. Here is the structure
id | pid | uid
1 5 2
2 2 3
3 2 4
4 2 6
5 3 7
In tree structure it would look this way. This is just an example, their could be many more nodes.
2
/ | \
3 4 6
/
7
So in php and mysql I fetch all that data and save it to an array.
I want to traverse that array to determine e.g. the number of id's in a particular level and I want to be able to retrieve all nodes from one level.
How can I do that in php?
EDIT
This is how I create my array:
foreach ( $resultSet as $row ) {
$entry = array();
$entry['id'] = $row->id;
$entry['uid'] = $row->uid;
$entry['rid'] = $row->rid;
$entry['date'] = $row->date;
$entries [] = $entry;
}
And this is how I create the tree now using the answer
$tree = array();
foreach($entries as $key){
$this->adj_tree($tree, $key);
}
return $tree;
But I get some strange output when I print $tree
Array ( [24] => Array ( [id] => 6 [uid] => 24 [rid] => 83 [date] => 2011-06-15
17:54:14 ) [] => Array ( [_children] => Array ( [0] => Array ( [id] => 6 [uid] => 24
[rid] => 83 [date] => 2011-06-15 17:54:14 ) [1] => Array ( [id] => 6 [uid] => 24 [rid] =>
83 [date] => 2011-06-15 17:54:14 ) ) ) )
But actually there should be one parent with the uid of 24 with two childs with rid 82 and 83