I want to combine arrays having same category id and question id.
Sample input:
$array = [
[
'category_id' => 1,
'question_id' => 1,
'option_id' => 2,
'title' => 'Do you wear glasses?',
'answer' => 'no'
],
[
'category_id' => 1,
'question_id' => 2,
'option_id' => 3,
'title' => 'Your hobbies ?',
'answer' => 'movies'
],
[
'category_id' => 1,
'question_id' => 4,
'option_id' => 8,
'title' => 'what is your status?',
'answer' => 'single'
],
[
'category_id' => 1,
'question_id' => 2,
'option_id' => 1,
'title' => 'Your hobbies ?',
'answer' => 'travel'
],
];
The 2nd and 4th arrays contain the same question (same category id and same question id), so I would like to have their answer values merged together as a comma-separated string.
Desired result:
Array(
[0] => Array
(
[category_id] => 1
[question_id] => 1
[option_id] => 2
[title] => Do you wear glasses?
[answer] => no
)
[1] => Array
(
[category_id] => 1
[question_id] => 2
[option_id] => 3
[title] => Your hobbies ?
[answer] => movies,travel
)
[2] => Array
(
[category_id] => 1
[question_id] => 4
[option_id] => 8
[title] => what is your status?
[answer] => single
)
)
It is okay that the option id and title are overwritten while grouping because I am not using the option id and the title values will be identical within each group.