3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = [ [ 'id' => 1, 'user_id' => 9788, 'contact_id' => 10083, 'product_id' => 1, 'category_id' => 23, 'grade' => 'A', 'point' => 89, ], [ 'id' => 1, 'user_id' => 9788, 'contact_id' => 10083, 'product_id' => 2, 'category_id' => 27, 'grade' => 'A', 'point' => 89, ] ]; $groupedArr = array_reduce($arr, function ($a, $c) { $a[$c['user_id']] = [ 'user_id' => $c['user_id'], 'contact_id' => $c['contact_id'], 'attributes' => array_merge([[ 'product_id' => $c['product_id'], 'category_id' => $c['category_id'] ]], $a[$c['user_id']]['attributes'] ?? []), 'grade' => $c['grade'], 'point' => $c['point'] ]; return $a; }, []); echo json_encode(array_values($groupedArr), JSON_PRETTY_PRINT);

preferences:
77.46 ms | 898 KiB | 5 Q