return group_concat data as array

Viewed 23943

I want to return values I retrieve from the db using group_concat as an array of data. Is it possible to do this in the mysql query? Or do I need to explode the data into an array?

GROUP_CONCAT(sh.hold_id) as holds

returns this

[holds] => 3,4

I want it to return:

[holds] => array(3,4)
5 Answers

If you need to do it on the MySQL level, and then you may probably parse it to an object. You can do the following

SELECT CONCAT("[", GROUP_CONCAT(category.name), "]") AS categories
From categories

It is possible to return mysql JSON array like so,

json_array(GROUP_CONCAT(sh.hold_id)) as holds

Refer docs for further info.

Related