I've been going through this tutorial on nested sets: http://www.sitepoint.com/article/1105/2
And I'm having a problem. Their method for "getting the tree" is nice, but they use this stack method for printing multiples spaces in front of nested items. Instead of just printing, I need the whole tree to be in a nested array I can pass around to various functions (specifically in this case, I'm going to use the nested array to generate a javascript tree browser with expanding and collapsing nodes - but that's not important).
So, the flat array is listed below, and below that is the array I want. If anyone could be of help, that would be greatly appreciated. I'm having trouble wrapping my head around it!
Also, as I was writing this post, I realized I could recursively look through the array for children, but that would require a foreach loop going through the entire array looking for children for *each* node... not very efficient. So I programmed it, and tried to modify the array as I found children to delete the childen (so they wouldn't ever get looped through more than once).
I'm thinking, there has to be a better way. It's certainly twisting my brain all up! So anyway, first we have the original array, then the array I want, then the code I used to get the array that I want, but hoping I can figure a more elegant way to get it.
Code:Array ( [0] => Array ( [cat_id] => 1 [parent_id] => [cat_name] => null test [cat_description] => null null null null null [lft] => 1 [rgt] => 8 ) [1] => Array ( [cat_id] => 5 [parent_id] => 1 [cat_name] => sub cat #1 [cat_description] => sub cat #1 [lft] => 2 [rgt] => 5 ) [2] => Array ( [cat_id] => 7 [parent_id] => 5 [cat_name] => sub sub cat #1 [cat_description] => sub sub cat #1 [lft] => 3 [rgt] => 4 ) [3] => Array ( [cat_id] => 6 [parent_id] => 1 [cat_name] => sub cat #2 [cat_description] => sub cat #2 [lft] => 6 [rgt] => 7 ) [4] => Array ( [cat_id] => 10 [parent_id] => [cat_name] => parent Cat [cat_description] => parent Cat [lft] => 9 [rgt] => 12 ) [5] => Array ( [cat_id] => 11 [parent_id] => 10 [cat_name] => kid of parent Cat [cat_description] => kid of parent Cat [lft] => 10 [rgt] => 11 ) )Code:Array ( [0] => Array ( [cat_id] => 1 [parent_id] => [cat_name] => null test [cat_description] => null null null null null [children] => Array ( [0] => Array ( [cat_id] => 5 [parent_id] => 1 [cat_name] => sub cat #1 [cat_description] => sub cat #1 [children] => Array ( [0] => Array ( [cat_id] => 7 [parent_id] => 5 [cat_name] => sub sub cat #1 [cat_description] => sub sub cat #1 [children] => Array ( ) ) ) ) [1] => Array ( [cat_id] => 6 [parent_id] => 1 [cat_name] => sub cat #2 [cat_description] => sub cat #2 [children] => Array ( ) ) ) ) [1] => Array ( [cat_id] => 10 [parent_id] => [cat_name] => parent Cat [cat_description] => parent Cat [children] => Array ( [0] => Array ( [cat_id] => 11 [parent_id] => 10 [cat_name] => kid of parent Cat [cat_description] => kid of parent Cat [children] => Array ( ) ) ) ) )Thanks in advance for any help.PHP Code:$nested_cats = nest_cats($cats);
function nest_cats($cats) {
$levels = array();
$i = 0;
foreach($cats as $cat) {
if($cat['parent_id'] == null) {
unset($cat['lft']);
unset($cat['rgt']);
$levels[$i] = $cat;
$levels[$i]['children'] = get_children($cat['cat_id'], $cats);
$i++;
}
}
return $levels;
}
function get_children($id, $cats) {
$return = array();
$i = 0;
foreach($cats as $cat_id => $cat) {
if($cat['parent_id'] == $id) {
unset($cat['lft']);
unset($cat['rgt']);
$return[$i] = $cat;
unset($cats[$cat_id]);
$return[$i]['children'] = get_children($cat['cat_id'], $cats);
$i++;
}
}
return $return;
}



Thanks again for your comments.


Bookmarks