Answer the question
In order to leave comments, you need to log in
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' => []
]
]
]
]
]
]
]
];
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question