Есть массив (представляет собой дерево категорий):
$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' => []
]
]
]
]
]
]
]
];
Мне нужно согласно этому массиву сформировать url адреса
те
/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/
...
Помогите пожалуйста, всю голову с рекурсией сломал
пытался из нее возвратить массив и потом каждому его элементу применить implode и склеить через /
не вышло
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;
}