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.

4

2 回答 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;
   }
}

类似的东西

于 2013-06-03T17:25:58.483 回答
0

我设法做到了:

$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:从数组列表创建数组树

于 2013-06-19T16:14:48.797 回答