title: PHP: calculate array values by array
body: <p>This may be simple to some of you guys but I'm really dummy in php. All what I need is just to transform a multidimensional array and calculate some of it's sub array values.</p>
  
  <pre><code>  Array
      (
          [0] =&gt; Array
              (
                  [date] =&gt; 2014-10-30
                  [mission] =&gt; one
                  [point] =&gt; 10
              )
  
          [1] =&gt; Array
              (
                  [date] =&gt; 2014-10-31
                  [mission] =&gt; five
                  [point] =&gt; 10
              )
  
          [2] =&gt; Array
              (
                  [date] =&gt; 2014-11-19
                  [mission] =&gt; one
                  [point] =&gt; 8
              )
  </code></pre>
  
  <p>And the output would be like:</p>
  
  <pre><code> Array
  (
      [one] =&gt; Array
          (
              [mission] =&gt; one
              [point] =&gt; 18 // sum all points where [mission] =&gt; one
              [count] =&gt; 2
              [everage] =&gt; 9 // 18/2
          )
  
      [five] =&gt; Array
          (
              [mission] =&gt; five
              [point] =&gt; 10
              [count] =&gt; 1
              [everage] =&gt; 10
          )
  </code></pre>
  
  <p>It is simple to get the sum of <code>[point]</code> values using <code>foreach</code> but troubles begin when I try to get <code>count</code> of arrays with same <code>[mission]</code> value. Thats my code:</p>
  
  <pre><code>foreach($missionsarray as $row) {
    if(!isset($newarray[ $row['mission'] ])) {
         $newarray[ $row['mission'] ] = $row;
         $newarray[ $row['mission'] ]['count'] = count($row['point']);
         continue ;
     }
     $newarray[ $row['mission'] ]['point'] += $row['point'];
  }
  
  print_r($newarray);
  </code></pre>