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 work with PHP. I have an array, made like this : (the level attribute is the level of the branch in the tree I want to make)

Array (
    [0] => stdClass Object
            (
                    [value] => Array
                            (
                                    [name] => Dog
                                    [level] => 1
                            )
            )
    [1] => stdClass Object
            (
                    [value] => Array
                            (
                                    [name] => Yorkshire
                                    [level] => 2
                            )
            )

    [2] => stdClass Object
            (
                    [value] => Array
                            (
                                    [name] => Rottweiler
                                    [level] => 2
                            )
            )

    [3] => stdClass Object
            (
                    [value] => Array
                            (
                                    [name] => Cat
                                    [level] => 1
                            )
            )
)

My goal is to make some kind of tree array from it, something like this :

Array (
    [0] => stdClass Object
            (
                    [value] => Array
                            (
                                    [name] => Dog
                                    [level] => 1
                            )
            )

            Array (

                    [1] => stdClass Object
                            (
                                    [value] => Array
                                            (
                                                    [name] => Yorkshire
                                                    [level] => 2
                                            )
                            )

                    [2] => stdClass Object
                            (
                                    [value] => Array
                                            (
                                                    [name] => Rottweiler
                                                    [level] => 2
                                            )
                            )

    )

    [3] => stdClass Object
            (
                    [value] => Array
                            (
                                    [name] => Cat
                                    [level] => 1
                            )
            )
)

But I really can't manage to do it. I even have issues to see the algorithm! Any help would be greatly appreciated.

share|improve this question
    
You'll need some kind of id to tell you who is the parent from this child. Becouse i know the lvl but i don't know each lvl 1 this lvl 2 belong. –  Guerra Jun 3 '13 at 17:18
    
I thought about this too, but the tree is already "ordered". I mean that it comes like : level 1 level 11 level 12 level 2 level 3 level 31 level 32 –  Frank Jun 3 '13 at 17:19
    
understood, if you can keep this order aways your solution will be kinda of : (look answer) –  Guerra Jun 3 '13 at 17:21

2 Answers 2

$treelist = array();
foreach($list as $key=>$l){
   if($l['level'] == 1){
      $l['childs'] = array();
      $treelist[] = $l;
   }else{
      $treelist[count($treelist-1)]['childs'][] = $l;
   }
}

something like that

share|improve this answer
up vote 0 down vote accepted

I managed to do it this way :

$new = array();
foreach ($arr as $a){
    $new[$a['parentid']][] = $a;
} 
$tree = createTree($new, $new[0]); // changed
print_r($tree);

function createTree(&$list, $parent){
    $tree = array();
    foreach ($parent as $k=>$l){
        if(isset($list[$l['id']])){
            $l['children'] = createTree($list, $list[$l['id']]);
        }
        $tree[] = $l;
    } 
    return $tree;
}

from arthur : create array tree from array list

share|improve this answer

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.