【发布时间】:2018-04-12 02:53:03
【问题描述】:
我有这段代码可以列出类别和子类别,但我需要它在 childCategories 中,我已经设法添加到第二级,如果超过第二级,我将无法添加它。场景会是这样的:-
父类别是“电子”,子类别是“智能手机”和“笔记本电脑”,在智能手机中,有华硕和三星,笔记本电脑也是如此。在笔记本电脑类别中有“Alienware 系列”和“Vostro 系列”。
电子产品
-> 智能手机
-->华硕
--> 三星
-> 笔记本电脑
--> 生命值
--> 戴尔 ---> Alienware 系列、Vostro 系列
我希望数组是这样的......
Array
(
[categoryId] => 3
[title] => Electronic
[bannerImage] => elect.jpg
[childCategories] => Array
(
Array
(
[categoryId] => 4
[title] => Smartphone
[bannerImage] => smartphone-banner.png
[childCategories] => Array
(
Array
(
[categoryId] => 4
[title] => Asus
[bannerImage] =>
[childCategories] => Array
(
)
),
Array
(
[categoryId] => 5
[title] => Samsung
[bannerImage] =>
[childCategories] => Array
(
)
)
),
Array
(
[categoryId] => 6
[title] => Laptops
[bannerImage] =>
[childCategories] => Array
(
Array
(
[categoryId] => 7
[title] => HP
[bannerImage] =>
[childCategories] => Array
(
)
),
Array
(
[categoryId] => 8
[title] => Dell
[bannerImage] =>
[childCategories] => Array
(
Array
(
[categoryId] => 9
[title] => Alienware Series
[bannerImage] =>
[childCategories] => Array
(
)
),
Array
(
[categoryId] => 10
[title] => Vostro Series
[bannerImage] =>
[childCategories] => Array
(
)
)
),
)
)
)
)
)
这是我的代码:-
if($category->getData('level') > 2) {
$temp_array = array();
$paths = explode("/", $category->getData('path'));
for ($x=0; $x <= count($paths); $x++) {
if($x == 0 || $x == 1) {
unset($paths[$x]);
}
else {
$temp_array['categoryId'] = $category->getData('entity_id');
$temp_array['title'] = $category->getData('name');
$temp_array['bannerImage'] = $category->getData('image');
$temp_array['childCategories'] = array();
$temp_array['path'] = $category->getData('path');
$temp_array['parent'] = $category->getData('parent_id');
$temp_array['level'] = $category->getData('level');
$category_array[$category->getData('parent_id')]['childCategories'][$category->getData('entity_id')] = $temp_array;
}
}
}
请注意,如果将来会有更多的子类别要添加。因此它不能是正常的数组分配。
更新 3
{
"3": {
"categoryId": "3",
"title": "Electronic",
"bannerImage": "elect.jpg",
"childCategories": [],
"parent": "2",
"path": "1/2/3"
},
"4": {
"categoryId": "4",
"title": "Smartphone",
"bannerImage": "smartphone-banner.png",
"childCategories": [],
"parent": "3",
"path": "1/2/3/4"
},
"5": {
"categoryId": "5",
"title": "Laptop",
"bannerImage": null,
"childCategories": [],
"parent": "3",
"path": "1/2/3/5"
},
"6": {
"categoryId": "6",
"title": "Health",
"bannerImage": null,
"childCategories": [],
"parent": "2",
"path": "1/2/6"
},
"7": {
"categoryId": "7",
"title": "Vitamin",
"bannerImage": null,
"childCategories": [],
"parent": "6",
"path": "1/2/6/7"
},
"9": {
"categoryId": "9",
"title": "Fashion",
"bannerImage": null,
"childCategories": [],
"parent": "2",
"path": "1/2/9"
},
"10": {
"categoryId": "10",
"title": "For Her",
"bannerImage": null,
"childCategories": [],
"parent": "9",
"path": "1/2/9/10"
},
"11": {
"categoryId": "11",
"title": "For Him",
"bannerImage": null,
"childCategories": [],
"parent": "9",
"path": "1/2/9/11"
},
"12": {
"categoryId": "12",
"title": "Samsung",
"bannerImage": null,
"childCategories": [],
"parent": "4",
"path": "1/2/3/4/12"
},
"13": {
"categoryId": "13",
"title": "Dell",
"bannerImage": null,
"childCategories": [],
"parent": "5",
"path": "1/2/3/5/13"
},
"14": {
"categoryId": "14",
"title": "Alienware",
"bannerImage": null,
"childCategories": [],
"parent": "13",
"path": "1/2/3/5/13/14"
},
"15": {
"categoryId": "15",
"title": "Vostro",
"bannerImage": null,
"childCategories": [],
"parent": "13",
"path": "1/2/3/5/13/15"
},
"timestamp": "2017-10-31 04:20:20"
}
【问题讨论】:
-
@mickmackusa 箭头只是类别外观的直观视图。
-
是的,我意识到这一点。但是,如果我要尝试帮助您,我还必须花时间编写一组最小/完整的示例输入,以便我可以开发和测试我的解决方案。 -- 这使得回答你的问题没有吸引力(对大多数志愿者而言)。
-
@mickmackusa 注意,会为你生成一个 json 格式。
-
@mickmackusa Json 已生成
-
您的 json 数据已经正确嵌套,请提供未嵌套的输入,而不是预期的输出。我与该示例 json 数据无关。
标签: php recursion multidimensional-array