Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a typical nested tree model and I want to build an array with a 'children' array based on the levels or depths, but it doesn't seem to be working for me. Here's what I have now:

while($this->tax->getTreeNext($nodes)) 
{

    $level = $this->tax->getTreeLevel($nodes);

    if($level != 0){
        echo $level . '-' . $current_level;
        if($level > $current_level){
            $terms[$i] = array(
                    'term_id' => $terms[$i-1]['term_id'],
                    'name' => $terms[$i-1]['name'],
                    'level' => $terms[$i-1]['level'],
                        'children'  => array(
                        'term_id'   => $nodes['row']['term_id'], 
                        'name'      => $nodes['row']['name'], 
                        'level'     => $level,               
                        )
                );

            unset($terms[$i-1]);
        }else{

            $terms[$i] = array(
                'term_id'   => $nodes['row']['term_id'], 
                'name'      => $nodes['row']['name'], 
                'level'     => $level
            );
        }

        $current_level = $level;
        $i++;
    }
}

This works for a single child, but not if the children have children... any suggestions how to fix this?

Thank you!

Edit:

This is the latest that appears to be close to working:

function process(&$arr, &$prev_sub = null, $cur_depth = 1) {

  $cur_sub = array();
    while($line = current($arr)){
        if($line['depth'] < $cur_depth){
            return $cur_sub; 
        }elseif($line['depth'] > $cur_depth){


            $prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1 );

        }else{

            $cur_sub[$line['term_id']] = array('term_id' => $line['term_id'], 'name' => $line['name']);
            $prev_sub =& $cur_sub[$line['term_id']];
            next($arr);
        }
    }
  return $cur_sub;
 }

The tree is passed into this with depth values associated to each node. The current problem is with the elseif($line['depth'] > $cur_depth). If a node has children, it only returns arrays for the children, but it does not include that nodes name or term_id.

Thank you!

share|improve this question
3  
recursion instead of iteration. –  usoban Sep 26 '11 at 18:34
    
Not quite sure I get it... could you post an example? –  dave Sep 26 '11 at 18:47
    
Could you elaborate a little on your data structure? How is your tree defined and stored? –  watcher Sep 26 '11 at 19:04
    
It's using two columns lft and rgt, pretty much a standard nested set from what I understand –  dave Sep 26 '11 at 19:13
add comment

1 Answer

up vote 1 down vote accepted

Since I do not really get how your current data structures looks like, take a look at this trivial example of traversing a tree.

$treeRoot = $this->tax->getRoot();

$result = traverse($treeRoot, 0);

function traverse($root, $level){
 $arr = array();

 $arr['term_id'] = $root['row']['term_id'];
 $arr['name'] = $root['row']['name'];
 $arr['level'] = $level;

 while($child = $root->getNextChild()){
  $arr['children'][] = traverse($child, $level+1);
 }

 return $arr;
}

So you start at the tree root, and fill the first level of array. Then you continue with root's children, but you go one level deeper. You do exactly the same thing as with the root, you fill in the data and go to children's children. When you reach the bottom of the tree, the last (grandgrandgrand)child finds out it's got no children left, so it just returns itself(normal array) to its parent. That parent returns itself back to its parent, and so on, till you reach the root again.

And voilà, you've got yourself a nested array. Typically you'd leave this kind of structure as a tree, but since I do not know what exactly do you want your result to be (your provided no example), use the above code as a reference for your own implementation.

share|improve this answer
    
Thanks.. this seems exactly right, it's giving me an endless loop though.. not sure –  dave Sep 26 '11 at 21:16
    
Hm, strange. while() should stop it at the lowest level since leaf does not have any children, so the loop shouldn't be even executed. Try debugging and find out where it cycles, and update the question, I'll look into it. –  usoban Sep 27 '11 at 9:24
    
Thank you, I've updated the question to the latest –  dave Sep 27 '11 at 13:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.