<?php $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' ], ]; $result = []; foreach ($array as $row) { $compositeKey = $row['category_id'] . '_' . $row['question_id']; if (!isset($result[$compositeKey])) { $result[$compositeKey] = $row; } else { $result[$compositeKey]['answer'] .= ",{$row['answer']}"; } } var_export( array_values( $result ) );
You have javascript disabled. You will not be able to edit any code.