0

I need help to sum values in an array. The array is called $results and I need to sum the Total_Sales values for each Month. EDIT - I have the loop below that I need to get the results; however I get notices. How can I improve the code so that I don't get those notices?

Here's the array:

$results

array(4) {
  [0]=>
  array(3) {
    ["Month"]=>
    string(1) "1"
    ["Country"]=>
    string(2) "AU"
    ["Total_Sales"]=>
    string(7) "9095.70"
  }
  [1]=>
  array(3) {
    ["Month"]=>
    string(1) "1"
    ["Country"]=>
    string(2) "CA"
    ["Total_Sales"]=>
    string(9) "113993.00"
  }
  [2]=>
  array(3) {
    ["Month"]=>
    string(1) "2"
    ["Country"]=>
    string(2) "AU"
    ["Total_Sales"]=>
    string(7) "7393.65"
  }
  [4]=>
  array(3) {
    ["Month"]=>
    string(1) "2"
    ["Country"]=>
    string(2) "CA"
    ["Total_Sales"]=>
    string(9) "100279.43"
  }

Here is what I need:

array(2) {
  [1]=>
  array(2) {
    ["MONTH"]=>
    string(1) "1"
    ["Total_Sales"]=>
    float(123088.7)
  }
  [2]=>
  array(2) {
    ["MONTH"]=>
    string(1) "2"
    ["Total_Sales"]=>
    float(107673.08)
  }

I know I need to do a loop but not sure where to go from here.

$newarr=array();
foreach($results as $key) {

}

Edit: This loop gets me the results I need but throws Notices that I don't want.

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

Notices in the results

Notice: Undefined index: Total_Sales in /var/www/html/analytics/views/sales_year_line_data.php on line 134

Notice: Undefined index: Total_Sales in /var/www/html/analytics/views/sales_year_line_data.php on line 134

Cory
  • 173
  • 3
  • 11
  • Do a var_dump on your $key and see what you get. – Jason K Oct 01 '15 at 16:53
  • 1
    You could do that in php but really just change your query to your database, just add `GROUP BY country, month`. Php will be hard coded ugly and slow, use the power of your database ! – delmalki Oct 01 '15 at 16:57
  • 1
    Possible duplicate of [PHP Array\_Sum on multi dimensional array](http://stackoverflow.com/questions/1404422/php-array-sum-on-multi-dimensional-array) ; http://stackoverflow.com/questions/12838729/multidimensional-array-array-sum – zod Oct 01 '15 at 17:03
  • @delmalki - I would like to do the GROUP BY in the sql query, however I had to make some changes to the array after the initial query to account for currency exchange rates for several countries which was from a difference source and not contained in the database. – Cory Oct 01 '15 at 17:58

2 Answers2

0

inside your loop you just add the values:

$newarr=array();
foreach($results as $val) {
  // index your new array by month so you can easily add to total sales
  $month = $val['Month'];
  $newarr[$month]['Month'] = $month;
  $newarr[$month]['Total_Sales'] += $val['Total_Sales'];
}

// to get rid of your month-based index, use array_values
$finalarray = array_values($newarr);
Marty Mulligan
  • 1,134
  • 7
  • 9
  • array(1) { [0]=> array(2) { ["Month"]=> NULL ["Total_Sales"]=> float(9516340.9739569) } } – Cory Oct 01 '15 at 18:02
  • Thanks but that doesn't get me the array that I need. See what I have above in the questions. – Cory Oct 01 '15 at 18:03
0

I gather the keys of the array you need are the month integers (as I can see it starts from 1).

Let's try that:

$totals = array();

foreach ($results as $result) {
    $currentMonth = $result['Month'];
    // For the first record for a given month, we need to add an "empty" record
    if (!array_key_exists($currentMonth, $totals)) {
        $totals[$currentMonth] = array(
            'MONTH' => $currentMonth,
            'Total_Sales' => 0,
        );
    }

    // Then we can sum totals
   $totals[$currentMonth]['Total_Sales'] += $result['Total_Sales'];
}

var_dump($results) gives me:

array (size=2)
  1 => 
    array (size=2)
      'MONTH' => string '1' (length=1)
      'Total_Sales' => float 123088.7
  2 => 
    array (size=2)
      'MONTH' => string '2' (length=1)
      'Total_Sales' => float 107673.08
  • array(1) { [""]=> array(2) { ["MONTH"]=> NULL ["Total_Sales"]=> float(9513755.6339569) } } – Cory Oct 01 '15 at 17:25
  • Thanks but that doesn't get me the array that I need. See what I have above. – Cory Oct 01 '15 at 17:27
  • I'm not sure really, I've tested that again and it seems to work fine. I've added the output to my ticket. Do you think you could have implemented it wrongly? Could you give more details about your code? – Alexandre Leprêtre Oct 02 '15 at 10:23