<?php
$data = [
[
'question_category' => 'Engagement',
'result' => '6.6667',
'employee_first_name' => 'Person A',
],
[
'question_category' => 'Tenure',
'result' => '7.6667',
'employee_first_name' => 'Person A',
],
[
'question_category' => 'Engagement',
'result' => '7.6667',
'employee_first_name' => 'Person B',
],
[
'question_category' => 'Tenure',
'result' => '8.3333',
'employee_first_name' => 'Person B',
],
];
$grouped = [];
foreach($data as $row){
// I think this is the mapping? If anything else exists,
// this will throw
$xOrY = match($row['question_category']){
'Engagement' => 'x',
'Tenure' => 'y',
};
// Group by name
$grouped[$row['employee_first_name']][$xOrY] = $row['result'];
}
// Remove the names (optional)
$grouped = array_values($grouped);
var_dump($grouped);
preferences:
23.76 ms | 405 KiB | 5 Q