简体   繁体   中英

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

"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 . What it does it is it concatenates its row values into single row and separated by a comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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