title: How to GROUP BY and SUM PHP Array?
body: <p>How to GROUP BY AND SUM this Array by <code>evaluation_category_id</code></p>
  
  <pre><code>Array
  (
   [0] =&gt; Array
      (
          [id] =&gt; 60
          [evaluation_category_id] =&gt; 3
          [score] =&gt; 15
          [itemMaxPoint] =&gt; 20
      )
   [1] =&gt; Array
      (
          [id] =&gt; 61
          [evaluation_category_id] =&gt; 2
          [score] =&gt; 10
          [itemMaxPoint] =&gt; 20
      )
  
   [2] =&gt; Array
      (
          [id] =&gt; 62
          [evaluation_category_id] =&gt; 1
          [score] =&gt; 5
          [itemMaxPoint] =&gt; 20
      )
  
    [3] =&gt; Array
      (
          [id] =&gt; 63
          [evaluation_category_id] =&gt; 1
          [score] =&gt; 50
          [itemMaxPoint] =&gt; 200
      )
  
    [4] =&gt; Array
      (
          [id] =&gt; 64
          [evaluation_category_id] =&gt; 2
          [score] =&gt; 150
          [itemMaxPoint] =&gt; 200
      )
  
    [5] =&gt; Array
      (
          [id] =&gt; 65
          [evaluation_category_id] =&gt; 3
          [score] =&gt; 30
          [itemMaxPoint] =&gt; 50
      )
      .
      .
      .
   )
  </code></pre>
  
  <p>So that i get array like this</p>
  
  <pre><code> Array
    (
     [0] =&gt; Array
        (
  
      [evaluation_category_id] =&gt; 3
      [score] =&gt; 45
      [itemMaxPoint] =&gt; 70
     )
  
     [1] =&gt; Array
        (
      [evaluation_category_id] =&gt; 2
      [score] =&gt; 160
      [itemMaxPoint] =&gt; 220
     )
  
     [2] =&gt; Array
        (
      [evaluation_category_id] =&gt; 1
      [score] =&gt; 55
      [itemMaxPoint] =&gt; 220
     )
  } 
  </code></pre>
  
  <p>i have tried this but its not working .please correct me where i am doing wrong</p>
  
  <pre><code> public function test($data) {
      $groups = array();
      foreach ($data as $item) {
          $key = $item['evaluation_category_id'];
          if (!isset($groups[$key])) {
              $groups[$key] = array(
                  'id' =&gt; $key,
                  'score' =&gt; $item['score'],
                  'itemMaxPoint' =&gt; $item['itemMaxPoint'],
              );
          } else {
              $groups[$key]['score'] = $groups[$key]['score'] + $item['score'];
              $groups[$key]['itemMaxPoint'] = $groups[$key]['itemMaxPoint'] +$item['itemMaxPoint'];
          }
      }
      return $groups;
  }
  </code></pre>
  
  <p>the Output is</p>
  
  <pre><code>Array
  (
   [2] =&gt; Array
      (
          [id] =&gt; 2
          [score] =&gt; 121 //121 because the given array is different.but its actually SUM all values of score
          [itemMaxPoint] =&gt; 300
      )
  
  )
  </code></pre>