【问题标题】:How to GROUP BY and SUM PHP Array?如何 GROUP BY 和 SUM PHP 数组?
【发布时间】:2013-02-06 17:14:27
【问题描述】:

如何通过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
    )
    .
    .
    .
 )

所以我得到这样的数组

 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
   )
} 

我试过了,但它不起作用。请纠正我做错的地方

 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;
}

输出是

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

)

【问题讨论】:

标签: php arrays group-by sorting


【解决方案1】:

我已经解决了。

public function getDateWiseScore($data) {
    $groups = array();
    foreach ($data as $item) {
        $key = $item['evaluation_category_id'];
        if (!array_key_exists($key, $groups)) {
            $groups[$key] = array(
                'id' => $item['evaluation_category_id'],
                '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;
}

【讨论】:

    【解决方案2】:

    你可以做一个 Foreach,为相等的键生成数组,然后为你想要求和的每个数组值生成 array_sum() :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 2017-06-09
      • 2017-10-20
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多