A
A
Alexander Pankov2021-02-12 09:23:19
PHP
Alexander Pankov, 2021-02-12 09:23:19

How to traverse the tree and compose all its paths (category urls)?

There is an array (represents a tree of categories):

$arTree = [
    1 => [
        'id' => 1,
        'slug' => 'top category 1',
        'parent' => -1,
        'children' => [
            2 => [
                'id' => 1,
                'slug' => '2th category 1',
                'parent' => 1,
                'children' => [
                    3 => [
                        'id' => 3,
                        'slug' => '3th category 1',
                        'parent' => 2,
                        'children' => [
                            4 => [
                                'id' => 4,
                                'slug' => '4th category 1',
                                'parent' => 3,
                                'children' => []
                            ],
                            5 => [
                                'id' => 5,
                                'slug' => '4th category 2',
                                'parent' => 3,
                                'children' => []
                            ]
                        ]
                    ],
                    9 => [
                        'id' => 9,
                        'slug' => '3th category 2',
                        'parent' => 2,
                        'children' => [
                            12 => [
                                'id' => 10,
                                'slug' => '4th category 3',
                                'parent' => 9,
                                'children' => []
                            ],
                            11 => [
                                'id' => 11,
                                'slug' => '4th category 4',
                                'parent' => 9,
                                'children' => []
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ],
    1045 => [
        'id' => 1045,
        'slug' => 'top category 2',
        'parent' => -1,
        'children' => [
            1046 => [
                'id' => 1046,
                'slug' => '2th category 1045',
                'parent' => 1045,
                'children' => [
                    1047 => [
                        'id' => 1047,
                        'slug' => '3th category 1',
                        'parent' => 1046,
                        'children' => [
                            1048 => [
                                'id' => 1048,
                                'slug' => '4th category 1',
                                'parent' => 1047,
                                'children' => []
                            ],
                            1049 => [
                                'id' => 1049,
                                'slug' => '4th category 2',
                                'parent' => 1047,
                                'children' => []
                            ]
                        ]
                    ],
                    1050 => [
                        'id' => 1050,
                        'slug' => '3th category 2',
                        'parent' => 1046,
                        'children' => [
                            1051 => [
                                'id' => 1051,
                                'slug' => '4th category 3',
                                'parent' => 1050,
                                'children' => []
                            ],
                            1052 => [
                                'id' => 1052,
                                'slug' => '4th category 4',
                                'parent' => 1050,
                                'children' => []
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];


According to this array, I need to form the url addresses of
those
/top category 1/
/top category 1/2th category 1/
/top category 1/2th category 1/3th category 1/
/top category 1/2th category 1/3th category 1/4th category 1/
/top category 1/2th category 1/3th category 1/4th category 2/
/top category 1/2th category 1/3th category 2/
/top category 1/2th category 1/3th category 2/4th category 3/
/top category 1/2th category 1/3th category 2/4th category 4/
...

Please help me, I broke my whole head with recursion,
I tried to return an array from it and then apply implode to each of its elements and glue them through /
did not work

foreach ($arTree as $arTopCat) {
    getURLs($arTopCat);
}

function getURLs(array $arCat)
{
    if ($arCat['children']) {
        $A[] = $arCat['slug']; //что я делаю, не понимаю
        foreach ($arCat['children'] as $cats) {
            $B[] = getURLs($cats); //что я делаю, не понимаю
        }
    } else {
        return $arCat['slug'];
    }
    
    return $B;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-02-12
@PankovAlxndr

function getURLs($arr, $path = []) {
  $urls = [];

  foreach ($arr as $item) {
    array_push($path, $item['slug']);
    array_push($urls, '/'.implode('/', $path).'/', ...getURLs($item['children'], $path));
    array_pop($path);
  }

  return $urls;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question