1

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.

3
  • 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. Commented Jun 3, 2013 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 Commented Jun 3, 2013 at 17:19
  • understood, if you can keep this order aways your solution will be kinda of : (look answer) Commented Jun 3, 2013 at 17:21

2 Answers 2

1
$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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.