title: php - sum values in an array
body: <p>I have an array called $results that I need to sum the Total_Sales per Month.  I have the code below that gives me the results I need but I also get notices.  How do I improve the code? </p>
  
  <h2>$results</h2>
  
  <pre><code>array(4) {
    [0]=&gt;
    array(3) {
      ["Month"]=&gt;
      string(1) "1"
      ["Country"]=&gt;
      string(2) "AU"
      ["Total_Sales"]=&gt;
      string(7) "9095.70"
    }
    [1]=&gt;
    array(3) {
      ["Month"]=&gt;
      string(1) "1"
      ["Country"]=&gt;
      string(2) "CA"
      ["Total_Sales"]=&gt;
      string(9) "113993.00"
    }
    [2]=&gt;
    array(3) {
      ["Month"]=&gt;
      string(1) "2"
      ["Country"]=&gt;
      string(2) "AU"
      ["Total_Sales"]=&gt;
      string(7) "7393.65"
    }
    [4]=&gt;
    array(3) {
      ["Month"]=&gt;
      string(1) "2"
      ["Country"]=&gt;
      string(2) "CA"
      ["Total_Sales"]=&gt;
      string(9) "100279.43"
    }
  </code></pre>
  
  <p>Here's my code so far.  The array that it returns are the results that I need but I get notices</p>
  
  <h2>Code</h2>
  
  <pre><code>$newarr=array();
  foreach($results as $value) {
      $Month = $value['MONTH'];
      $Total_Sales = $value['Total_Sales'];
      array_key_exists( $Month, $newarr ) ? $newarr[$Month]['MONTH'] = $Month : $newarr[$Month]['MONTH'] = 0;
      array_key_exists( $Month, $newarr ) ? $newarr[$Month]['Total_Sales']+=$Total_Sales : $newarr[$Month]['Total_Sales'] = 0;
  }
  </code></pre>
  
  <h2>$newarr</h2>
  
  <pre><code>array(2) {
    [1]=&gt;
    array(2) {
      ["MONTH"]=&gt;
      string(1) "1"
      ["Total_Sales"]=&gt;
      float(123088.7)
    }
    [2]=&gt;
    array(2) {
      ["MONTH"]=&gt;
      string(1) "2"
      ["Total_Sales"]=&gt;
      float(107673.08)
    }
  </code></pre>
  
  <h2>Notices</h2>
  
  <p>Notice: Undefined index: Total_Sales in /var/www/html/analytics/views/sales_year_line_data.php on line 120</p>
  
  <p>Notice: Undefined index: Total_Sales in /var/www/html/analytics/views/sales_year_line_data.php on line 120</p>