0

I have an array that looks like this:

Array (
    [0] => Array
    (
        [term_id] => 23
        [name] => testasdf
        [depth] => 1
    )
    [1] => Array
    (
        [term_id] => 26
        [name] => asdf
        [depth] => 2
    )
    [2] => Array
    (
        [term_id] => 31
        [name] => Another level deep
        [depth] => 3
    )
    [3] => Array
    (
        [term_id] => 32
        [name] => Another level deep
        [depth] => 2
    )
    [4] => Array
    (
        [term_id] => 24
        [name] => testasdf
        [depth] => 1
    )
    [5] => Array
    (
        [term_id] => 27
        [name] => asdf
        [depth] => 1
    )
)

Here is the recursive function that I'm using, it works except in some cases (where the depth is greater it seems.

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;
}

This is how the results look:

Array
(
    [23] => Array
    (
        [26] => Array
        (
            [31] => Array
            (
                [term_id] => 31
                [name] => Another level deep
            )
        )
        [32] => Array
        (
            [term_id] => 32
            [name] => Another level deep
        )
    )
    [24] => Array
    (
        [term_id] => 24
        [name] => testasdf
    )
    [27] => Array
    (
        [term_id] => 27
        [name] => asdf
    )
)

Any idea how I can have it so the term_id and name is shown for all depths?

1 Answer 1

1

try this:

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

  $cur_sub = array();
    while($line = current($arr)){
        if($line['depth'] < $cur_depth){
            return $cur_sub; 
        }
        if($line['depth'] > $cur_depth){
            $prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1 );

        }

            $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;
}
1
  • This actually matched up the right ID's, I think before the term_id's being returned in the wrong order, but it still doesn't show the term_id and name values for the first two nodes, it seems any node that has children, doesn't get the term_id and name array added Commented Sep 27, 2011 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.