键名称的多维数组层次结构

时间:2019-05-14 17:26:30

标签: php arrays multidimensional-array path

我想知道如何通过标识符Key NAME(仅PHP的值)来获得层次结构。我已经尝试过一些爆破功能,但没有成功。我感谢任何帮助。谢谢
这是一个例子:

$treeArray = (Array
(
    [0] => Array
        (
            [name] => S-ATLANTICO-1
            [id] => 1HIk_jh2GHo2VnBbUI8c3P9cADY4NnKQ5
            [parents] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => TESTE
                            [id] => 1EYi_CF7gjANq_MPnUOkquJI609Jkhzf0
                            [parents] => 1HIk_jh2GHo2VnBbUI8c3P9cADY4NnKQ5
                        )

                    [1] => Array
                        (
                            [name] => SAPO
                            [id] => 1I8QxJiMa11U2s4ncPxyqfdCPk_6dQ9Tl
                            [parents] => 1HIk_jh2GHo2VnBbUI8c3P9cADY4NnKQ5
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => SAPO-1
                                            [id] => 1KGzjcy79TCKp-c6T1Xxm5WqswXhqFlb7
                                            [parents] => 1I8QxJiMa11U2s4ncPxyqfdCPk_6dQ9Tl
                                        )

                                    [1] => Array
                                        (
                                            [name] => carlos.csv
                                            [id] => 1eHU_r5GJCXualVQMhurd6FwOoD_h3hHG
                                            [parents] => 1I8QxJiMa11U2s4ncPxyqfdCPk_6dQ9Tl
                                        )

                                    [2] => Array
                                        (
                                            [name] => logo-news_sa.png
                                            [id] => 16HnsOxzDEow710jNfda7Mtt-8qsLwSeG
                                            [parents] => 1I8QxJiMa11U2s4ncPxyqfdCPk_6dQ9Tl
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [name] => DOCUMENTOS
                            [id] => 1viFriBa2GaSxiLG8nDYIDd-sFl545T31
                            [parents] => 1HIk_jh2GHo2VnBbUI8c3P9cADY4NnKQ5
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => carlos-excel
                                            [id] => 16YyMy3F9QMzRT5XtCMVJygKDEVVSRpAVmAjh0XU3luY
                                            [parents] => 1viFriBa2GaSxiLG8nDYIDd-sFl545T31
                                        )

                                    [1] => Array
                                        (
                                            [name] => carlos-excel.xlsx
                                            [id] => 1GbJ9YmwuRiUmnT8jRJ9pLZI7acqO4eu-
                                            [parents] => 1viFriBa2GaSxiLG8nDYIDd-sFl545T31
                                        )

                                    [2] => Array
                                        (
                                            [name] => SAPO
                                            [id] => 1DRBPJHPxRSeaa7zC8yF-UgGbo9tpwOsOxAusfEuGtsY
                                            [parents] => 1viFriBa2GaSxiLG8nDYIDd-sFl545T31
                                        )

                                    [3] => Array
                                        (
                                            [name] => PRECIOS
                                            [id] => 1S0lNS7bKOK6wBxhCZcB7uaNBXA1GMqlxwtTdSBc4f9U
                                            [parents] => 1viFriBa2GaSxiLG8nDYIDd-sFl545T31
                                        )

                                )

                        )

                )

        )

)
);

此结果可以:在数组中或在屏幕上逐行打印。

S-ATLANTICO-1/
S-ATLANTICO-1/TESTE
S-ATLANTICO-1/SAPO
S-ATLANTICO-1/SAPO/SAPO-1
S-ATLANTICO-1/SAPO/carlos.csv
S-ATLANTICO-1/SAPO/logo-news_sa.png
S-ATLANTICO-1/DOCUMENTOS
S-ATLANTICO-1/DOCUMENTOS/carlos-excel
S-ATLANTICO-1/DOCUMENTOS/carlos-excel.xlsx
S-ATLANTICO-1/DOCUMENTOS/SAPO
S-ATLANTICO-1/DOCUMENTOS/PRECIOS

2 个答案:

答案 0 :(得分:1)

这应该是这样的:

function rec($arr, $prefix ="") {
    if ($prefix != "") $prefix .= "/";
    foreach($arr as $e) {
        echo $prefix . $e['name'];
        if (!empty($e['children']))
            rec($e['children'], $prefix . $e['name']);
    }
}

我不在计算机上,所以仅此伪代码...

答案 1 :(得分:0)

$array = [
    ['App', 'Classes', 'Auth'],
    ['App', 'Classes', 'Auth'],
    ['App', 'Classes', 'Middleware'],
    ['App', 'Classes', 'Phone'],
    ['App', 'Classes', 'Auth'],
    ['App', 'Mail'],
    ['App', 'Mail', 'Sender'],
    ['App', 'Box'],
    ['Bla', 'bli'],
];

var_dump(arrayToNamespace($array));

function arrayToNamespace($array) {
    $newArr = array();

    foreach ($array as $nsArr) {
        $wns = &$newArr;
        foreach ($nsArr as $ns) {
            if (!isset($wns[$ns])) {
                $wns[$ns] = array();
            }
            $wns = &$wns[$ns];
        }
    }
    return $newArr;
}