I'm strugling with building an array out of a product category array. This array is a tree array with parents and children. No I selecting one of the children, and I want to build the permalink towards this child.
I'm almost there, but somewhere I'm making a mistake and I can't figure it out.
So lets say I have this category array.
$cat = [
0 => [
"id" => "32",
"shop_id" => "19",
"parent_id" => "0",
"name" => "T-shirts",
"permalink" => "t-shirts",
"image" => "category/image.jpg",
"description" => "",
"order" => "0",
"children" => [
0 => [
"id" => "33",
"shop_id" => "19",
"parent_id" => "32",
"name" => "Dames",
"permalink" => "t-shirts/dames",
"image" => "category/image.jpg",
"description" => "",
"order" => "0",
"children" => [
0 => [
"id" => "38",
"shop_id" => "19",
"parent_id" => "33",
"name" => "V-hals",
"permalink" => "t-shirts/dames/v-hals",
"image" => "category/image.jpg",
"description" => "",
"order" => "0",
"children" => [
0 => [
"id" => "40",
"shop_id" => "19",
"parent_id" => "38",
"name" => "Rood",
"permalink" => "t-shirts/dames/v-hals/rood",
"image" => "",
"description" => "",
"order" => "0",
"children" => [
0 => [
"id" => "47",
"shop_id" => "19",
"parent_id" => "40",
"name" => "Lange mouw",
"permalink" => "t-shirts/dames/v-hals/rood/lange-mouw",
"image" => "",
"description" => "",
"order" => "0",
"children" => null,
]
]
]
]
]
]
]
]
]
];
Now I'm at category_id 38 and I want to build the breadcrumb. To accomplish that, I need the name of each category, and the permalink.
To accomplish that, I have flattend this array so they are all at the same level. At that point I'm using the following function:
function build_breadcrumb_from_category($categories, $pid)
{
$return = [];
foreach ($categories as $category) {
if ($pid == $category['id']) {
$return[] = [$category['name'], $category['permalink']];
if ($category['parent_id'] > 0) {
# Page has parents
$return[] = build_breadcrumb_from_category($categories, $category['parent_id']);
}
}
}
return array_reverse($return);
}
But that gives me a kind of "tree" array again.
$breadcrumb = build_breadcrumb_from_category($flat_categories, 38);
$breadcrumb = [
0 => [
0 => [
0 => [
0 => [
0 => [
0 => "T-shirts",
1 => "t-shirts",
],
],
1 => [
0 => "Dames",
1 => "t-shirts/dames",
],
],
1 => [
0 => "V-hals",
1 => "t-shirts/dames/v-hals",
]
],
1 => [
0 => "Rood",
1 => "t-shirts/dames/v-hals/rood",
],
],
1 => [
0 => "Lange mouw",
1 => "t-shirts/dames/v-hals/rood/lange-mouw",
],
];
I don't understand how I can get this array flat. How can I get a nice array, just one level deep where I can do a foreach.
Desired output
$breadcrumbs = [
[
0 => "T-shirts",
1 => "t-shirts",
],
[
0 => "Dames",
1 => "t-shirts/dames",
],
[
0 => "V-hals",
1 => "t-shirts/dames/v-hals",
],
]
$outputarray, but flat, all on the same level.