I am having trouble getting well formatted results from my mysql query.
Here is the database:
customers table
id name
1 Tim
2 Lauren
3 Prada
tanks table
id c_id location
1 1 Living Room
2 2 Living Room
3 2 Kitchen
4 3 Bedroom
5 3 Hallway
fish table
id t_id color
1 1 red
2 1 blue
3 2 white
4 2 green
5 2 black
6 3 red
7 3 orange
8 4 white
9 4 blue
10 5 black
Here is my query:
SELECT * FROM customers
LEFT JOIN tanks ON (tanks.c_id = customers.id)
LEFT JOIN fish ON (fish.t_id = tanks.id)
GROUP BY customers.id, tanks.id;
Here is the result that I am getting:
id name id c_id location id t_id color
1 Tim 1 1 Living Room 1 1 red
2 Lauren 2 2 Living Room 3 2 white
2 Lauren 3 2 Kitchen 6 3 red
3 Prada 4 3 Bedroom 8 4 white
3 Prada 5 3 Hallway 10 5 black
Here is the format that I am trying to get:
[0] => Array
(
[customers] => Array
(
[0] => Array
(
[id] => 2
[title] => Lauren
[tanks] => Array
(
[0] => Array
(
[id] => 2
[location] => Living Room
[fish] => Array
(
[0] => Array
(
[id] => 3
[color] => white
)
[1] => Array
(
[id] => 4
[color] => green
)
[2] => Array
(
[id] => 5
[color] => black
)
)
)
[1] => Array
(
[id] => 3
[location] => Kitchen
[fish] => Array
(
[0] => Array
(
[id] => 6
[color] => red
)
[1] => Array
(
[id] => 7
[color] => orange
)
)
)
)
)
)
)
Can I get the desired result from one query or do I need to do multiple queries and then join them together in PHP?
Any help would be appreciated!
You can’t get this output directly from mysql because it always returns row based outputs. Bu after you get the result, you can convert the row-type array to your desired array with a bridge method.