用于转换多维数组的递归函数

时间:2014-10-13 13:31:04

标签: php arrays recursion multidimensional-array

在使用Cake PHP进行SQL查询后,我有一个数组,它返回一棵树。我不是我的数组的维数。

我希望将其转换为与jstree一起使用。我用递归功能打架,但我没有成功。

你能帮我吗?

我的原始数组如下所示:

Array
(
[0] => Array
    (
        [Confsave] => Array
            (
                [id] => 815
                [Name] => 1
                [parent_id] =>
                [lft] => 1
                [rght] => 30
            )

        [children] => Array
            (
                [0] => Array
                    (
                        [Confsave] => Array
                            (
                                [id] => 816
                                [Name] => 2
                                [parent_id] => 815
                                [lft] => 2
                                [rght] => 15
                            )

                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [Confsave] => Array
                                            (
                                                [id] => 817
                                                [parent_id] => 816
                                                [lft] => 3
                                                [rght] => 8
                                            )

                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [Confsave] => Array
                                                            (
                                                                [id] => 818
                                                                [Name] => 4
                                                                [parent_id] => 817
                                                                [lft] => 4
                                                                [rght] => 5
                                                            )

                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                                [1] => Array
                                                    (
                                                        [Confsave] => Array
                                                            (
                                                                [id] => 819
                                                                [Name] => 5
                                                                [parent_id] => 817
                                                                [lft] => 6
                                                                [rght] => 7
                                                            )

                                                        [children] => Array
                                                            (
                                                            )
                                                    )
                                            )
                                    )
                            )
                    )
            )
    )
)

我希望有这样的事情:

Array
(
[0] => Array
    (
        [text] => 1
        [id] => 815
        [children] => Array
            (
                [0] => Array
                    (
                        [text] => 2
                        [id] => 816
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [text] => 3
                                        [id] => 817
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [text] => 4
                                                        [id] => 818
                                                    )
                                            )
                                    )
                                [1] => Array
                                    (
                                        [text] => 5
                                        [id] => 819
                                    )
                            )
                    )
            )
    )
)

我尝试过像这样的递归函数,但我没有成功

private function buildTree(array $elements) {

    $branch=array();
    foreach ($elements as $element){

        $branch[]=$element['Confsave']['Name'];
        if(is_array($element['children'])){
            $this->buildTree($element);
        }

    }
    return $branch;

}

编辑: 经过测试和评论,我的功能现在是

private function buildTree(array $elements) {

    $branch=array();
    foreach ($elements as $element){

        $branch[]=$element['Confsave']['Name'];
        if(is_array($element['children'])){ 
            $this->buildTree($element['children']);
        }

    }
    return $branch;
}

当我正在调试时,我可以看到我为每个孩子(我想要的)做了我的功能。但我不知道如何制作新的数组()

1 个答案:

答案 0 :(得分:0)

这对我有用......

$array = buildTree($array); 

print_r($array); 

function buildTree(array $parent) { 
   $branch = array();  
   foreach ($parent as $index => $element){  
      $node = array();   
      $node['id']   = $element['Confsave']['id'];
      $node['text'] = isset($element['Confsave']['Name']) ?
                            $element['Confsave']['Name']  : 'no name';
      $node['children'] = buildTree($element['children']); 
      $branch[] = $node; 
   }
   return $branch; 
}

我的初始测试数组......

Array
(
    [0] => Array
        (
            [Confsave] => Array
                (
                    [id] => 999
                    [Name] => 999
                    [parent_id] => 
                    [lft] => 1
                    [rght] => 30
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [Confsave] => Array
                                (
                                    [id] => 777
                                    [Name] => 777
                                    [parent_id] => 999
                                    [lft] => 1
                                    [rght] => 30
                                )

                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [Confsave] => Array
                                (
                                    [id] => 888
                                    [Name] => 888
                                    [parent_id] => 999
                                    [lft] => 1
                                    [rght] => 30
                                )

                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [Confsave] => Array
                (
                    [id] => 815
                    [Name] => 1
                    [parent_id] => 
                    [lft] => 1
                    [rght] => 30
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [Confsave] => Array
                                (
                                    [id] => 816
                                    [Name] => 2
                                    [parent_id] => 815
                                    [lft] => 2
                                    [rght] => 15
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [Confsave] => Array
                                                (
                                                    [id] => 817
                                                    [parent_id] => 816
                                                    [lft] => 3
                                                    [rght] => 8
                                                )

                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [Confsave] => Array
                                                                (
                                                                    [id] => 818
                                                                    [Name] => 4
                                                                    [parent_id] => 817
                                                                    [lft] => 4
                                                                    [rght] => 5
                                                                )

                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [Confsave] => Array
                                                                (
                                                                    [id] => 819
                                                                    [Name] => 5
                                                                    [parent_id] => 817
                                                                    [lft] => 6
                                                                    [rght] => 7
                                                                )

                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我的结果数组......

Array
(
    [0] => Array
        (
            [id] => 999
            [text] => 999
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 777
                            [text] => 777
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 888
                            [text] => 888
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 815
            [text] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 816
                            [text] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 817
                                            [text] => no name
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 818
                                                            [text] => 4
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 819
                                                            [text] => 5
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)