Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

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.

share|improve this answer
I have seen someone did that but don't know how. This Group_Concat can't help – 巫妖王 Nov 18 '12 at 5:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.