3 回答
TA贡献1852条经验 获得超7个赞
下面的代码可能会帮助您处理原始数组(您可以通过 toArray() 来完成),
$parentCategories = [6, 4, 3, 2];
$result = [];
foreach ($parentCategories as $key => $pv) {
foreach ($childCategories as $cv) {
if ($pv == $cv['id']) {
if (empty($result[$cv['id']]['name'])) {
$result[$cv['id']]['data']['id'] = $pv;
$result[$cv['id']]['data']['name'] = $cv['name'];
}
$result[$cv['id']]['children'][] = $cv['subcategory'];
}
}
}
输出:-
(
[name] => Cartridges
[id] => 17579
)
[6] => Array
(
[name] => Tincture
[id] => 17644
)
[7] => Array
(
[name] => Capsules
[id] => 17645
)
[8] => Array
(
[name] => Vaporizers
[id] => 42
)
[9] => Array
(
[name] => Shirts
[id] => 44
)
[10] => Array
(
[name] => CBD Bath Bomb
[id] => 19461
)
[11] => Array
(
[name] => Edibles
[id] => 17580
)
)
)
[4] => Array
(
[data] => Array
(
[id] => 4
[name] => Infused Edible
)
[children] => Array
(
[0] => Array
(
[name] => Gummies
[id] => 26
)
[1] => Array
(
[name] => Chocolate Bars
[id] => 25
)
[2] => Array
(
[name] => Baked Goods
[id] => 23
)
[3] => Array
(
[name] => Tincture
[id] => 29
)
[4] => Array
(
[name] => Cooking Oil
[id] => 18864
)
[5] => Array
(
[name] => Sweets
[id] => 27
)
[6] => Array
(
[name] => Capsules/Pills
[id] => 22
)
)
)
[3] => Array
(
[data] => Array
(
[id] => 3
[name] => Cartridges / Pens
)
[children] => Array
(
[0] => Array
(
[name] => Solventless
[id] => 18051
)
[1] => Array
(
[name] => BHO
[id] => 10
)
)
)
[2] => Array
(
[data] => Array
(
[id] => 2
[name] => Concentrate
)
[children] => Array
(
[0] => Array
(
[name] => Live Resin
[id] => 15
)
[1] => Array
(
[name] => Distillate
[id] => 18005
)
[2] => Array
(
[name] => Sauce
[id] => 18767
)
[3] => Array
(
[name] => RSO
[id] => 18865
)
[4] => Array
(
[name] => Sugar (Each)
[id] => 18876
)
)
)
)
我从逻辑上得到脚本的输出,
foreach ($result as $key => $value) {
echo "<ul>";
echo "<li>" . $value['data']['name'] . '(' . 'id=' . $value['data']['id'] . ')';
echo "<ul>";
foreach ($value['children'] as $k => $value1) {
echo "<li>";
if ($k == 0) {
echo $value1['name'] . "(" . 'categoryid = ' . $value['data']['id'] . ', subcategoryid = ' . $value1['id'] . ")";
} else {
echo $value1['name'];
}
echo "</li>";
}
echo "</ul></li></ul>";
}
输出为
<ul>
<li>Non Medicated(id=6)
<ul>
<li>Glass(categoryid = 6, subcategoryid = 409)</li>
<li>Accesories</li>
<li>Lip Balm</li>
<li>Lotion</li>
<li>Distillate</li>
<li>Cartridges</li>
<li>Tincture</li>
<li>Capsules</li>
<li>Vaporizers</li>
<li>Shirts</li>
<li>CBD Bath Bomb</li>
<li>Edibles</li>
</ul>
</li>
</ul>
<ul>
<li>Infused Edible(id=4)
<ul>
<li>Gummies(categoryid = 4, subcategoryid = 26)</li>
<li>Chocolate Bars</li>
<li>Baked Goods</li>
<li>Tincture</li>
<li>Cooking Oil</li>
<li>Sweets</li>
<li>Capsules/Pills</li>
</ul>
</li>
</ul>
<ul>
<li>Cartridges / Pens(id=3)
<ul>
<li>Solventless(categoryid = 3, subcategoryid = 18051)</li>
<li>BHO</li>
</ul>
</li>
</ul>
<ul>
<li>Concentrate(id=2)
<ul>
<li>Live Resin(categoryid = 2, subcategoryid = 15)</li>
<li>Distillate</li>
<li>Sauce</li>
<li>RSO</li>
<li>Sugar (Each)</li>
</ul>
</li>
</ul>
TA贡献1812条经验 获得超5个赞
$categories我像你一样创建了一个假对象( )。
希望这可以帮助:
<?php
$categories = array();
$obj0 = new stdClass;
$obj0->id = 123;
$obj0->name = "Non Medicated";
$obja = new stdClass;
$obja->name = "Glass";
$obja->id = 409;
$obj0->subcategory =$obja;
$categories[0] = $obj0;
$obj1 = new stdClass;
$obj1->id = 124;
$obj1->name = "Non Medicated";
$obj1a = new stdClass;
$obj1a->name = "Capsules";
$obj1a->id = 410;
$obj1->subcategory =$obj1a;
$categories[1] = $obj1;
$obj2 = new stdClass;
$obj2->id = 125;
$obj2->name = "Non Medicated";
$obj2a = new stdClass;
$obj2a->name = "Capsules";
$obj2a->id = 411;
$obj2->subcategory =$obj2a;
$categories[2] = $obj2;
$toRet =array();
foreach($categories as $category){
$cat = $category->name;
$toRet[$cat][] = $category->subcategory;
}
var_dump($toRet);
这是var_dump函数的输出:
array(1) { ["Non Medicated"]=> array(3) { [0]=> object(stdClass)#2 (2) { ["name"]=> string(5) "Glass" ["id"]=> int(409) } [1]=> object(stdClass)#4 (2) { ["name"]=> string(8) "Capsules" ["id"]=> int(410) } [2]=> object(stdClass)#6 (2) { ["name"]=> string(8) "Capsules" ["id"]=> int(411) } } }
TA贡献1155条经验 获得超0个赞
不确定我是否理解你想要的,但如果你只需要显示类别和子类别,你可以做这样的事情吗?
foreach($categories as $key => $category){
echo $category->name;
echo $category->id;
foreach($category->subcategory as $key2 => $subCategory){
echo $subCategory->name;
echo $subCategory->id;
echo $category->id;
}
}
- 3 回答
- 0 关注
- 152 浏览
添加回答
举报