php - MYSQL How group results from two columns like key/value pairs
one text
Solution:
FINAL SOLUTION
The most accurate query in order to get grouped values as key/value pairs was the following:
SELECT d.*,
(SELECT CAST(
CONCAT('[',
GROUP_CONCAT(
JSON_OBJECT(
dv2.value ,dv2.description
)
),
']')
AS JSON) FROM (SELECT * FROM dictionary_values dv WHERE dv.field_name_id = d.id) AS dv2)
AS d_value
FROM dictionary d;
END
Finally showing grouped data:
{
"id": "1",
"field_name": "address_type",
"description": "Address Type Indicator",
"category": "base",
"d_value": "[{\"F\": \"Company\"}, {\"G\": \"General Delivery\"}, {\"H\": \"Highway\"}, {\"P\": \"PO Box\"}, {\"R\": \"Rural Route\"}, {\"S\": \"Street\"}]"
}
Only remains parse d_value string removing \ with str_replace and convert into an array with str_split (PHP)
Source