问题描述
如何选择联接多个表并轻松地将其放入嵌套数组中?
How to select join multiple tables and get it in a nested array easily?
例如
Table 1 -School: SchoolID, SchoolName,PrincipalID
Talbe 2 - Principal: PrincipalID,PrincipalName
我想在PHP中获得嵌套数组,例如
I want to get a nested array in PHP like
"School": [
{
"SchoolID": "7",
"SchoolName": "New",
"Principal": {
"PrincipalID":"1",
"PrincipalName":"James"
}
}
问题是,如何使用自动方式而不是指定每个字段来嵌套数组并手动将其添加到数组中?甚至不知道实际的字段名称,而只是将多个表嵌套到一个嵌套数组中.
The problem is, how to get the nested array use a automatic way instead of specifying each field and add it into a array manually? without even kowning the actual field names, just nesting multiple tables into a nested array.
推荐答案
查询的结果只能是一个维数组.因此,换句话说,您无法创建多维结果.您需要在应用程序级别执行此操作(使用您自己的逻辑),但是有一个功能可以在某个时候为您提供帮助.它称为 GROUP_CONCAT
.它的作用是将其行值连接为单行并用逗号分隔.
the result of the query can be a single dimension array only. so in other words, you can't create a multidimensional result. You need to do that in the application level, (use your own logic) but there's a function that can help you sometime. It's called GROUP_CONCAT
. What it does it is it concatenates its row values into single row and separated by a comma.
- GROUP_CONCAT
- GROP_CONCAT example
这篇关于将表连接到嵌套数组[PHP/MYSQL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!