-1

I have an array that has multiple years of total sales for several countries. Below is small sample of the array. Some countries had sales in several different currencies but are now converted into the same currency. Now I need to add those total sales together where the Year, Month and Country values are the same but still keep the similar structure.

If values of ["Year"] & ["Month"] & ["Country"] match, then I need sum the values of ["Total_Sales"] and keep the same array structure.

I'm not sure where to begin. I'm sure there is a foreach loop or 2 involved.

Here is the array

array(6) {
  [0]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    float(8.25)   
  }
  [1]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    string(6) "5.50"
  }
  [2]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "US"
    ["Total_Sales"]=>
    string(9) "10.05"
  }
  [3]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(1) "1"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    float(9.50)
  }
  [4]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(1) "1"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    string(6) "15.00"
  [5]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(2) "1"
    ["Country"]=>
    string(2) "US"
    ["Total_Sales"]=>
    string(9) "6.00"
}

Here's the results that I would like:

array(4) {
  [0]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    float(13.75)
  }
  [1]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "US"
    ["Total_Sales"]=>
    string(9) "10.05"
  }
  [2]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(1) "1"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    float(24.50)
  }
  [3]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(2) "1"
    ["Country"]=>
    string(2) "US"
    ["Total_Sales"]=>
    string(9) "6.00"
}
Cory
  • 173
  • 3
  • 11

1 Answers1

1

do something like

foreach($currentArray as $value){
    $newArraykey = $value["country"]."-".$value["Year"]."-".$value["month"]
    if(isset($newArray[$newArraykey])){
        $newArray[$newArraykey]["Total_Sales"] = $newArray[$newArraykey]["Total_Sales"] + $value["Total_Sales"];
    }else{
        $newArray[$newArraykey] = $value;
    }
}

$newArray would be something like

array(4) {
  [NZ-2014-12]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    float(13.75)
  }
  [US-2014-12]=>
  array(4) {
    ["Year"]=>
    string(4) "2014"
    ["Month"]=>
    string(2) "12"
    ["Country"]=>
    string(2) "US"
    ["Total_Sales"]=>
    string(9) "10.05"
  }
  [NZ-2015-1]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(1) "1"
    ["Country"]=>
    string(2) "NZ"
    ["Total_Sales"]=>
    float(24.50)
  }
  [US-2015-1]=>
  array(4) {
    ["Year"]=>
    string(4) "2015"
    ["Month"]=>
    string(2) "1"
    ["Country"]=>
    string(2) "US"
    ["Total_Sales"]=>
    string(9) "6.00"
}

If you do not want the keys like country-year-month they use array_values($newArray) this would return exactly the same array you wanted.

Arpita
  • 1,386
  • 1
  • 15
  • 35
  • I have looked at something similar but couldn't get it to work right. The code does't seem to add up the Total_Sales value. Rather it removes the first match and returns the second with only it's total. – Cory Oct 09 '15 at 06:01
  • array(4) { ["NZ-2014-9"]=> float(13.75) ["US-2014-9"]=> string(9) "10.05" ["NZ-2015-9"]=> float(15.00) ["US-2015-9"]=> string(9) "6.00" } – Cory Oct 09 '15 at 06:01
  • there was some silly mistake in the code check that now. – Arpita Oct 09 '15 at 06:37