简体   繁体   中英

Making a “tree array” from an array in php

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.

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM