How to GROUP BY and SUM PHP Array? [duplicate]

I have solved it.

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

Leave a Comment