gpt4 book ai didi

php - 在 PHP 中创建对象和数组的复杂 JSON 嵌套

转载 作者:行者123 更新时间:2023-11-29 03:31:13 24 4
gpt4 key购买 nike

我的表格是这样的:

enter image description here

我需要用这种结构创建 JSON 对象:

    array=[
{
id: 2,
items: [
{
id: 12
},
{
id: 13
},
{
id: 14
}
]
},
{
id: 3,
items: [
{
id: 15
},
{
id: 16
},
{
id: 17
},
{
id: 18
},
{
id: 19
}
]
},
{
id: 4,
items: [
{
id: 20
},
{
id: 21
}
]
},
{
id: 5,
items: [
{
id: 22
}
]
}
];

因此,in object 必须是 parent 的 ID,in its items 必须是 child 的 ID。我必须使用哪个查询以及如何创建类似的结构?它还取决于这些对象的顺序。亲子必令。我的意思是,在我的查询中必须是这样的:

ORDER BY `order` ASC

提前致谢。

最佳答案

Self Join 可能是这项工作的解决方案。尝试类似的东西

SELECT  a.ID, b.ParentID FROM tester a, tester b WHERE a.ID = b.ID order by a.Order ASC

关于php - 在 PHP 中创建对象和数组的复杂 JSON 嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30323614/

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