平面数组到树阵列

时间:2014-01-25 21:25:13

标签: php arrays

我目前坚持将数组“flat”修改为树的数组结构。

我有一个类型的数组:

$folders = array(
    array('Name' => 'Archive', 'Value' => 'Archive', 'Attributes' => 32, 'Delimiters' => '/'),
    array('Name' => 'Folder1', 'Value' => 'Archive/Folder1', 'Attributes' => 32, 'Delimiters' => '/'),
    array('Name' => 'FolderA', 'Value' => 'Archive/Folder1/DossierA', 'Attributes' => 64, 'Delimiters' => '/'),
    array('Name' => 'Folder2', 'Value' => 'Archive/Folder2', 'Attributes' => 32, 'Delimiters' => '/'),
    array('Name' => 'FolderA', 'Value' => 'Archive/Folder2/FolderA', 'Attributes' => 32, 'Delimiters' => '/'),
    array('Name' => 'FolderB', 'Value' => 'Archive/Folder2/FolderA/FolderB', 'Attributes' => 64, 'Delimiters' => '/'),
);

我会得到一个类型的数组:

$treefolders = array(
    array('Name' => 'Archive', 'Value' => 'Archive', 'Attributes' => 32, 'Delimiters' => '/', 'subnode' => 
        array('Name' => 'Folder1', 'Value' => 'Archive/Folder1', 'Attributes' => 32, 'Delimiters' => '/', 'subnode' =>
            array('Name' => 'FolderA', 'Value' => 'Archive/Folder1/FolderA', 'Attributes' => 64, 'Delimiters' => '/')
        ),
        array('Name' => 'Folder2', 'Value' => 'Archive/Folder2', 'Attributes' => 32, 'Delimiters' => '/', 'subnode' => 
            array('Name' => 'FolderA', 'Value' => 'Archive/Folder2/FolderA', 'Attributes' => 32, 'Delimiters' => '/', 'subnode' =>
                array('Name' => 'FolderB', 'Value' => 'Archive/Folder2/FolderA/FolderB', 'Attributes' => 64, 'Delimiters' => '/')
            )
        )
    )
);

我不知道如何得到最后的结果。

你能帮助我吗?

2 个答案:

答案 0 :(得分:0)

这是不可能的,在PHP数组中不能用作数组键。您只能使用目录名作为密钥。

最佳解决方案是更改创建阵列的方式或重新考虑代码和/或数据结构。对您的问题可能有一个更简单的解决方案,可能会重新提出您的问题。请参阅XY problem

答案 1 :(得分:0)

对不起,我在第一篇文章中做了一些事情

这是我的数组“flat”的结构:

Array
(
    [0] => 
        Array
        (
            'Name' => 'Archive'
            'Value' => 'Archive'
            'Attributes' => 32
            'Delimiters' => '/'
        )    
    [1] =>
        Array
        (
            'Name' => 'Folder1'
            'Value' => 'Archive/Folder1'
            'Attributes' => 32
            'Delimiters' => '/'
        )    
    [2] =>
        Array
        (
            'Name' => 'FolderA'
            'Value' => 'Archive/Folder1/DossierA'
            'Attributes' => 64
            'Delimiters' => '/'
        )    
    [3] =>
        Array
        (
            'Name' => 'Folder2'
            'Value' => 'Archive/Folder2'
            'Attributes' => 32
            'Delimiters' => '/'
        )    
   [4] =>
        Array
        (
            'Name' => 'FolderA'
            'Value' => 'Archive/Folder2/FolderA'
            'Attributes' => 32
            'Delimiters' => '/'
        )    
   [5] =>
        Array
        (
            'Name' => 'FolderB'
            'Value' => 'Archive/Folder2FolderA/FolderB'
            'Attributes' => 64
            'Delimiters' => '/'
        )    
)

我正在寻找:

Array
(
    [0] => 
        Array
        (
            'Name' => 'Archive'
            'Value' => 'Archive'
            'Attributes' => 32
            'Delimiters' => '/'
            'SubNode' =>
                Array
                (
                    [0] => 
                        Array
                        (
                            'Name' => 'Folder1'
                            'Value' => 'Archive/Folder1'
                            'Attributes' => 32
                            'Delimiters' => '/'
                            'SubNode' =>
                                Array
                                (
                                    [0] => 
                                        Array
                                        (
                                            'Name' => 'FolderA'
                                            'Value' => 'Archive/Folder1/DossierA'
                                            'Attributes' => 64
                                            'Delimiters' => '/'
                                        )    
                                )    
                        )    
                    [1] =>
                        Array
                        (
                            'Name' => 'Folder2'
                            'Value' => 'Archive/Folder2'
                            'Attributes' => 32
                            'Delimiters' => '/'
                            'SubNode' =>
                                Array
                                (
                                    [0] => 
                                        Array
                                        (
                                            'Name' => 'FolderA'
                                            'Value' => 'Archive/Folder2/FolderA'
                                            'Attributes' => 32
                                            'Delimiters' => '/'
                                            'SubNode' =>
                                                Array
                                                (
                                                    [0] => 
                                                        Array
                                                        (
                                                            'Name' => 'FolderB'
                                                            'Value' => 'Archive/Folder2/FolderA/FolderB'
                                                            'Attributes' => 64
                                                            'Delimiters' => '/'
                                                        )    
                                                )    
                                        )    
                                )    
                        )    
                )    
        )    
)

通常它应该是可能的^^