1

I like to know how can I get the hierarchy by the identifier Key NAME, just the values with PHP. I already had try some implode functions but without success. I appreciated any help. Thanks
This is an example :

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

                                )

                        )

                )

        )

)
);

This result would be ok: in array or print in screen line by line.

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
4
  • 2
    You need to look at Recursion Commented May 14, 2019 at 17:39
  • @AbraCadaver awesome! Took me a sec to understand Commented May 14, 2019 at 17:42
  • Thanks. I already try that but without sucess Commented May 14, 2019 at 17:45
  • Then that's what you should be asking about... Commented May 14, 2019 at 19:09

2 Answers 2

1

This should be something like this:

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']);
    }
}

I not on computer so this pseudo code only...

Sign up to request clarification or add additional context in comments.

2 Comments

Slightly modified, but I think your idea works. +1 3v4l.org/UHpF7
@CarlosCosta update my post to check if add prefix /
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.