I'm really having trouble displaying JSON data with groupBy user_id conditions on the same data and displaying multiple data on certain data.
My Table:
+----+---------+------------+------------+-------------+-------+-------+
| id | user_id | contact_id | product_id | category_id | grade | point |
+----+---------+------------+------------+-------------+-------+-------+
| 1 | 9788 | 10083 | 1 | 23 | A | 89 |
+----+---------+------------+------------+-------------+-------+-------+
| 2 | 9788 | 10083 | 2 | 27 | A | 89 |
+----+---------+------------+------------+-------------+-------+-------+
PHP Script:
$arr = [];
foreach($data as $value){
$arr['user_id'] = $value['user_id'];
$arr['contact_id'] = $value['contact_id'];
$arr[$value['product_id']]['attributes']['products'] = $value['product_id'];
$arr[$value['category_id']]['attributes']['products'] = $value['category_id'];
$arr['grade'] = $value['grade'];
$arr['point'] = $value['point'];
}
The JSON output what I want:
"user_id" : 9788,
"contact_id" : 10083
"attributes": [
{
"products": [
{
"product_id": "1",
"category_id": "23"
},
{
"product_id": "2",
"category_id": "27"
}
]
}
],
"grade" : "A",
"point" : 89
The current output is:
{
"2": { "attributes": { "products": 1 } },
"4": { "attributes": { "products": 23 } },
"21": { "attributes": { "products": 2 } },
"25": { "attributes": { "products": 27 } },
"user_id": 9788,
"contact_id": 10083,
"grade": "A",
"point": 89
}