Hy,我有2个表,categories(id, name, slug)
和subcategories(id, name, slug, category)
。
我需要获取所有类别+子类别,但作为数组'字段'。我现在的询问是
SELECT c.id AS catId, c.name AS catName, c.slug AS catSlug, s.id AS subcatId, s.name AS subcatName, s.slug AS subcatSlug
FROM categories AS c
LEFT JOIN subcategories AS s ON c.id = s.category
结果就是这个
Array
(
[0] => stdClass Object
(
[catId] => 5
[catName] => calzature
[catSlug] => calzature
[subcatId] => 10
[subcatName] => eleganti
[subcatSlug] => eleganti
)
[1] => stdClass Object
(
[catId] => 5
[catName] => calzature
[catSlug] => calzature
[subcatId] => 11
[subcatName] => sportive
[subcatSlug] => sportive
)
....
)
我想将它们分组如下
Array
(
[0] => stdClass Object
(
[catId] => 5
[catName] => calzature
[catSlug] => calzature
[subcategories] => array(
[0] => array(
[subcatId] => 1,
[subcatName] => Subcategory name,
[subcatSlug] => subcategory-slug,
),
[1] => array(
[subcatId] => 2,
[subcatName] => Another Subcategory name,
[subcatSlug] => another-subcategory-slug,
),
.....
)
)
我的目标是创建一个下拉菜单,其中主<li>
是类别名称,辅助<li>
是子类别。
如果可能,我如何在一个查询中完成此操作?
谢谢。