I have a Laravel join database query which returns me this:
[
{
"post_id": 28,
"site_id": 16,
"url": "http://something.com",
"title": "Website title",
"post_created_at": "2015-07-06 02:40:01",
"post_updated_at": "2015-09-08 22:33:20",
"tag_map_id": 11,
"tag_id": 9,
"tag_name": "dog"
},
{
"post_id": 28,
"site_id": 16,
"url": "http://something.com",
"title": "Website title",
"post_created_at": "2015-07-06 02:40:01",
"post_updated_at": "2015-09-08 22:33:20",
"tag_map_id": 12,
"tag_id": 10,
"tag_name": "cat"
}
]
So there can be multiple results with same post_id but one post can also have multiple tags.
What I want, is to merge these multiple results into one JSON object which contains all the tag-related stuff in array. So there should be only one JSON-object per one post_id. Like the example below:
[
{
"post_id": 28,
"site_id": 16,
"url": "http://something.com",
"title": "Website title",
"post_created_at": "2015-07-06 02:40:01",
"post_updated_at": "2015-09-08 22:33:20",
"tag_map_id": [11, 12],
"tag_id": [9, 10],
"tag_name": ["dog", "cat"]
}
]
How can I achieve this merging in PHP?