在php中从数组中创建一个“树数组”

时间:2013-06-03 17:12:19

标签: php arrays algorithm tree

我使用PHP。我有一个数组,像这样:( level属性是我想要的树中分支的级别)

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
                            )
            )
)

我的目标是从中创建一些树形数组,如下所示:

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
                            )
            )
)

但我真的无法做到。我甚至有问题要看算法! 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

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

类似的东西

答案 1 :(得分: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;
}

来自亚瑟:create array tree from array list