简体   繁体   English

将表连接到嵌套数组[PHP / MYSQL]

[英]Join Table To a nested array [PHP/MYSQL]

How to select join multiple tables and get it in a nested array easily? 如何选择联接多个表并轻松地将其放入嵌套数组中?

For Example 例如

Table 1 -School: SchoolID, SchoolName,PrincipalID
Talbe 2 - Principal: PrincipalID,PrincipalName

I want to get a nested array in PHP like 我想在PHP中获得嵌套数组,例如

"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. 甚至不知道实际的字段名称,只需将多个表嵌套到一个嵌套数组中即可。

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 . 它称为GROUP_CONCAT What it does it is it concatenates its row values into single row and separated by a comma. 它的作用是将其行值连接为单行,并用逗号分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM