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"
}