I want to get an nested array from MySql with a query.
MySQL
+------+---------+--------+---------+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| id | title | kindof | website | categories | images |
+------+---------+--------+---------+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| 158 | prova 7 | design | wwww | Brass,Jewerly | image000.jpg,image001.jpg |
| 159 | Prova 8 | food | www | Italian Food,Korean Food | image000.jpg,image001.jpg, image002.jpg |
+------+---------+--------+---------+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
From my API I get this:
[
{
"id": 158,
"title": "prova 7",
"kindof": "design",
"website": "wwww",
"categories": "Brass,Jewerly",
"img_order": "0,1",
"images": "image000.jpg,image001.jpg"
},
{
"id": 159,
"title": "Prova 8",
"kindof": "food",
"website": "www",
"categories": "Italian Food,Korean Food",
"img_order": "0,1,2",
"images": "image000.jpg,image001.jpg,image002.jpg"
}
]
I need to get something like this:
[
{
"id": 158,
"title": "prova 7",
"kindof": "design",
"website": "wwww",
"categories": "Brass,Jewerly",
"images": [
{ position: "0", image: "image000.jpg" },
{ position: "1", image: "image001.jpg" }
]
},
{
"id": 159,
"title": "Prova 8",
"kindof": "food",
"website": "www",
"categories": "Italian Food,Korean Food",
"images": [
{ position: "0", image: "image000.jpg" },
{ position: "1", image: "image001.jpg" },
{ position: "2", image: "image002.jpg" }
]
}
]
the only idea I got is to create it maping what I do need once I got it.
Is there maybe a solution doing this with a query?
Thanks!