- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为从产品类别数组构建数组而苦苦挣扎。这个数组是一个有 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
并且我想构建面包屑。为此,我需要每个类别的 name
和 permalink
。
为了实现这一点,我将这个数组展平,使它们都处于同一水平。那时我正在使用以下功能:
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/
是否有办法获取所选 TreeView 节点的索引,或者他们是否有一个? 最佳答案 由于您要求“索引”只是为了能够查找与此项目关联的数据,因此您应该知道树控件可以保存您的数据。每个项目(TVITEM 结
我不太确定这个问题以前是否有人问过,因为它似乎太微不足道了,不太新鲜,但是我已经环顾四周一个小时了,还没有发现任何东西 我正在使用 ttk Treeview 来显示和分析表格。对于给定的函数,我希望能
我不太确定这个问题以前是否有人问过,因为它似乎太微不足道了,不太新鲜,但是我已经环顾四周一个小时了,还没有发现任何东西 我正在使用 ttk Treeview 来显示和分析表格。对于给定的函数,我希望能
我有一个复杂的 json 字符串,如下所示: { "id":"2016666", "dt":"2012", "object_extends":[ {
我用了JQuery TreeView在大模式下,我想折叠除 root 之外的所有项目,如何更改 jquery.treeview.js 来执行此操作? 最佳答案 嗯..也许不是最干净的解决方案,但似乎有
这听起来可能很奇怪,因为我无法找到我想做的确切术语。 我正在开发一个应用程序,它具有一组规则(易于转换为函数)和输入/输出对(不那么容易转换为代码),将允许构造规则树以应用于给定的输入达到给定的输出。
我是网络开发的菜鸟。我正在尝试创建一个树状的分层公司组织结构图。我尝试了两个谷歌的 visualization chart和 Mike Bostock 的 D3 Reingold tree . 我想要
我是一名优秀的程序员,十分优秀!