php - How to expand GROUP_CONCAT value? -


select     `s1`.`question`,     group_concat(quote(`so1`.`name`)) `answers`     `survey` `s1` inner join     `survey_option` `so1` on     `so1`.`survey_id` = `s1`.`id` group     `s1`.`id`; 

this query product question list answer quoted-comma separated string.

how expand answer string array?

the first thing comes mind explode("','", mb_substr($answers_str, 1, -1)).

are there gotchas should aware?

# run query select #   `s1`.`id`,     `s1`.`question`,     `so1`.`name`     `survey` `s1` inner join     `survey_option` `so1` on     `so1`.`survey_id` = `s1`.`id`  # fetch query $arr = array(); while($row = mysql_fetch_object($query))     $arr[$row->question][] = $row->name;  # if question's aren't unique, can fetch 'id' in query and: $arr = array(); while($row = mysql_fetch_object($query))     $arr[$row->id][] = $row; 

this asumes save query value $query. there's no point in doing group_concat if plan split them afterwards. using array , it's key values enough that.


Comments