Hướng dẫn php array group by key and sum - nhóm mảng php theo khóa và tổng

Cách nhóm theo và tổng hợp mảng này bằng evaluation_category_id

Array
(
 [0] => Array
    (
        [id] => 60
        [evaluation_category_id] => 3
        [score] => 15
        [itemMaxPoint] => 20
    )
 [1] => Array
    (
        [id] => 61
        [evaluation_category_id] => 2
        [score] => 10
        [itemMaxPoint] => 20
    )

 [2] => Array
    (
        [id] => 62
        [evaluation_category_id] => 1
        [score] => 5
        [itemMaxPoint] => 20
    )

  [3] => Array
    (
        [id] => 63
        [evaluation_category_id] => 1
        [score] => 50
        [itemMaxPoint] => 200
    )

  [4] => Array
    (
        [id] => 64
        [evaluation_category_id] => 2
        [score] => 150
        [itemMaxPoint] => 200
    )

  [5] => Array
    (
        [id] => 65
        [evaluation_category_id] => 3
        [score] => 30
        [itemMaxPoint] => 50
    )
    .
    .
    .
 )

Để tôi nhận được mảng như thế này

 Array
  (
   [0] => Array
      (

    [evaluation_category_id] => 3
    [score] => 45
    [itemMaxPoint] => 70
   )

   [1] => Array
      (
    [evaluation_category_id] => 2
    [score] => 160
    [itemMaxPoint] => 220
   )

   [2] => Array
      (
    [evaluation_category_id] => 1
    [score] => 55
    [itemMaxPoint] => 220
   )
} 

Tôi đã thử điều này nhưng nó không hoạt động. Xin vui lòng sửa tôi ở đâu tôi đang làm sai

 public function test($data) {
    $groups = array();
    foreach ($data as $item) {
        $key = $item['evaluation_category_id'];
        if (!isset($groups[$key])) {
            $groups[$key] = array(
                'id' => $key,
                'score' => $item['score'],
                'itemMaxPoint' => $item['itemMaxPoint'],
            );
        } else {
            $groups[$key]['score'] = $groups[$key]['score'] + $item['score'];
            $groups[$key]['itemMaxPoint'] = $groups[$key]['itemMaxPoint'] +$item['itemMaxPoint'];
        }
    }
    return $groups;
}

đầu ra là

Array
(
 [2] => Array
    (
        [id] => 2
        [score] => 121 //121 because the given array is different.but its actually SUM all values of score
        [itemMaxPoint] => 300
    )

)

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây.Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn.Tìm hiểu thêm về các ký tự unicode hai chiều

$ mảng = [array = [
['thuế' => '1', 'val' => 10],'tax'=>'1','val'=>10],
['thuế' => '1', 'val' => 20],'tax'=>'1','val'=>20],
['Thuế' => '2', 'Val' => 10]'tax'=>'2','val'=>10]
];
$ result = mảng_Reduce ($ mảng, hàm ($ carry, $ item) {result = array_reduce($array,function($carry,$item){
if (! isset ($ carry [$ item ['thuế']]))) {(!isset($carry[$item['tax']])){
$ mang [$ item ['thuế']] = ['thuế' => $ item ['thuế'], 'val' => $ item ['val']];carry[$item['tax']] = ['tax'=>$item['tax'],'val'=>$item['val']];
} khác {else {
$ mang [$ item ['thuế']] ['val'] += $ item ['val'];carry[$item['tax']]['val'] += $item['val'];
}
trả lại $ mang; $carry;
});
echo "Kết quả *****". PHP_EOL; "RESULT*****".PHP_EOL;
echo var_dump ($ result); var_dump($result);
/**
RESULT*****
mảng (2) {
[1] =>
mảng (2) {
["Thuế"] =>
Chuỗi (1) "1"
["Val"] =>
int (30)
}
[2] =>
mảng (2) {
["Thuế"] =>
Chuỗi (1) "1"
["Val"] =>
int (30)
}
[2] =>
Chuỗi (1) "2"