gpt4 book ai didi

PHP构建树形数组/平面数组的面包屑

转载 作者:行者123 更新时间:2023-12-04 06:10:26 24 4
gpt4 key购买 nike

我正在为从产品类别数组构建数组而苦苦挣扎。这个数组是一个有 parent 和 child 的树状数组。不,我选择其中一个 child ,我想为这个 child 建立永久链接。

我快到了,但我在某个地方犯了一个错误,我无法弄清楚。

假设我有这个类别数组。

$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,
]
]
]
]
]
]
]
]
]
];

现在我在 category_id 38 并且我想构建面包屑。为此,我需要每个类别的 namepermalink

为了实现这一点,我将这个数组展平,使它们都处于同一水平。那时我正在使用以下功能:

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

但这又给了我一种“树”数组。

$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",
],
];

我不明白如何让这个数组变平。我怎样才能得到一个漂亮的数组,只有一层深度,我可以在其中执行 foreach

期望的输出

   $breadcrumbs = [
[
0 => "T-shirts",
1 => "t-shirts",
],
[
0 => "Dames",
1 => "t-shirts/dames",
],
[
0 => "V-hals",
1 => "t-shirts/dames/v-hals",
],
]

最佳答案

您可以通过索引扁平化数组来优化它 - 现在搜索循环太多(如果您想要更好的解决方案,请发布您的扁平化功能)。不过,您拥有的功能不需要大的改变。您应该首先构建您的父结构,推送当前位置,然后将其返回到更高的范围(作为其父结构):

function build_breadcrumb_from_category($categories, $pid)
{
$result = [];
foreach ($categories as $category) {
if ($pid == $category['id']) {
if ($category['parent_id'] > 0) {
# Page has parents
$result = build_breadcrumb_from_category($categories, $category['parent_id']);
}

$result[] = [$category['name'], $category['permalink']];
}
}
return $result;
}

这是带有索引类别列表的解决方案 - 第一个函数将树展平,第二个函数使用其 id 索引来构建面包屑路径:

function category_index($category_tree)
{
$result = [];
foreach($category_tree as $category) {
if (!empty($category['children'])) {
$result = $result + category_index($category['children']);
}
unset($category['children']);
$result[$category['id']] = $category;
}
return $result;
}

function category_breadcrumb($category_index, $pid)
{
if (empty($category_index[$pid])) { return []; }

$category = $category_index[$pid];
$result = ($category['parent_id']) ? category_breadcrumb($category_index, $category['parent_id']) : [];
$result[] = [$category['name'], $category['permalink']];

return $result;
}

关于PHP构建树形数组/平面数组的面包屑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40982702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com