Answer the question
In order to leave comments, you need to log in
How to convert a two dimensional array returned by MySQL?
Colleagues, for example, there is a database with the following tables city, house with the following fields:
city:
-id
-name
house:
-id
-city_id
-heigh
-width
[
id => 1,
name => city1,
house => [
id => 1,
heigh => 100,
width => 200,
]
],
[
id => 1,
name => city1,
house => [
id => 2,
heigh => 200,
width => 300,
]
]
Answer the question
In order to leave comments, you need to log in
Do you have one house in the city?
Still, house must be an array.
You will have to use a loop. Therefore, it is better to do this:
1. A selection of cities
2. For each city, make a selection of houses, create an array with city fields and add house with a selection of houses to it, and add the final array of the city to the general array.
Alternatively, use grouping and the right combination of functions like json_arrayagg and json_object
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question