-1

I am trying to do this as efficiently as possible.

I have multiple arrays:

array1 = [
 "2018" => 
    [
        "JAN" => 100,
        "FEB" => 200,
        "MAR" => 300,
        "APR" => 400
    ]
]

array2 = [
 "2018" => 
    [
        "FEB" => 200,
        "MAR" => 300,
        "APR" => 400,
        "MAY" => 200,
    ]
]

array3 = [
 "2018" => 
    [
        "MAY" => 200,
        "JUN" => 100,
        "JUL" => 300,
        "AUG" => 400,
    ]
]

I want to add these arrays together with a desired output of Year/Month Totals:

sumArray = [
     "2018" => 
        [
            "JAN" => 100,
            "FEB" => 400,
            "MAR" => 600,
            "APR" => 800
            "MAY" => 400,
            "JUN" => 100,
            "JUL" => 300,
            "AUG" => 400,
        ]
    ]

I wanted to avoid mulitple foreach loops and figured there would be a better solution with array_map, array_walk or anything else. Anyone got ideas?

Thanks

Cybergei
  • 39
  • 1
  • 7
  • 3
    What's wrong with using `foreach(){}`? Also, why do you have `array1`, `array2`, and `array3`? Those should be sub-arrays of a parent array which you should be able to loop quite easily. Also, your variable declarations are missing dollar signs. Your question contains a lot of nonsense and zero effort to be honest; is this a random homework assignment? You should post what you've tried so far. – MonkeyZeus Nov 26 '18 at 13:26
  • Not sure of the tern *better solution*, as long as you don't do brute force `foreach()` repeatedly - it's the quicker solution. – Nigel Ren Nov 26 '18 at 13:27
  • check this link : https://stackoverflow.com/questions/1496682/how-to-sum-all-column-values-in-multi-dimensional-array – Hardik Solanki Nov 26 '18 at 13:53

2 Answers2

1

Here's a single foreach. But a triple ternary ifs:

$monthCodes = array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
foreach($monthCodes as $key => $monthCode){
  $add = 0;
  $add += ( isset($array1["2018"][$monthCode]) ) ?  $array1["2018"][$monthCode] : 0;
  $add += ( isset($array2["2018"][$monthCode]) ) ?  $array2["2018"][$monthCode] : 0;
  $add += ( isset($array3["2018"][$monthCode]) ) ?  $array3["2018"][$monthCode] : 0;
  if($add <> 0){
    $sumArray["2018"][$monthCode] = $add;
  }
}
Script47
  • 14,230
  • 4
  • 45
  • 66
Werner
  • 449
  • 4
  • 12
  • The OP seems new so I don't think it's wise to use the obscure `<>` in place of the more known `!=`. – Script47 Nov 26 '18 at 13:41
  • But then you also get months with 0 - I'm not sure it is part of the require output – dWinder Nov 26 '18 at 13:43
  • This code is perfect and exactly what I am looking for. Thank you very much for taking the time to answer me professionally and for having the experience to see what exactly I am doing and looking for. – Cybergei Nov 26 '18 at 14:06
1

I believe you need to use some kind of foreach to do this effective.

I create a new array with all arrays in it and loop the subarrays of it to sum the values.

// Add arrays that needs to be summed in the line below
$new = array_merge_recursive([$array1], [$array2], [$array3]);
foreach($new as $arr){
    foreach($arr as $year => $sub){
        foreach($sub as $month => $val){
            if(!isset($res[$year][$month])) $res[$year][$month] =0;
            $res[$year][$month] += $val;
        }
    }
}
var_dump($res);

https://3v4l.org/WHdDJi

Andreas
  • 23,610
  • 6
  • 30
  • 62