2

I'm trying to merge/sums 2 arrays that can contain integers or more arrays (themselves containing integer).

When the values are integers, I need to sum them in the final array. When the values are arrays, I need to loop through the values and sum them in the final array.

If a value or a sub-array exists only in 1 of the base array, it needs to be added in the sub-array of the final array. (This is what I can't do)..)

My arrays are like this:

 ARRAY 1
[1466859600] => Array
    (
        [TOTAL] => 27217
        [AAA] => Array
            (
                [FD_CDP] => 1746
                [LO_SC_MIC] => 4654
                [FD_ATS] => 893
                [CDP] => 40
                [SUPERVISION] => 9
                [CONTROL] => 4
                [ATS] => 4
                [EVT_ACK] => 3
            )

        [BBB] => Array
            (
                [FD_CDP] => 1376
                [LO_SC_MIC] => 4606
                [FD_ATS] => 826
                [FD_ATSS] => 451
                [LO_SFRC] => 4
                [FD_S2] => 259
                [2_LOSC] => 2
            )

        [CCC] => Array
            (
                [FD_CDP] => 1333
                [LO_SC_MIC] => 4725
                [FD_ATS] => 856
                [CONTROL] => 4
                [ATS] => 2
                [EVT_ACK] => 5
            )

 ARRAY 2
     [1466859600] => Array
    (
        [TOTAL] => 95406
        [AAA] => Array
            (
                [FD_ATSS] => 1719
                [LO_SC_MIC] => 16830
                [CONTROL] => 16
                [NEW] => 7
                [NOEL] => 206
            )

        [BBB] => Array
            (
                [SUPERVISION] => 23
                [CDP] => 158
                [CONTROL] => 40
                [2_LOSC] => 14
                [ATS] => 6
                [EVT_ACK] => 4
            )

        [CCC] => Array
            (
                [EVT_ACK] => 167
                [LO_SFRC] => 248
                [SUPERVISION] => 23
    )

I wrote a function like this :

    function sumArrayValues($array1, $array2)
        {
            foreach ($array1 as $key => $value) 
            {

                if (is_array($array1[$key]))
                    {
                        echo "it's an array\n I need to reloop\n";
                        sumArrayValues($array1[$key], $array2[$key]);
                    }
                else
                    {
                        echo "FIRST VALUE TO SUM\n";
                        print_r($array1[$key]."\n");
                        echo "SECOND VALUE TO SUM\n";
                        print_r($array2[$key]."\n");
                        $array1[$key] = (int)$array1[$key] +(int)$array2[$key];
                        echo "--------RESULT of SUM array1&2----------\n";
                    }
            }
        return $array1;

        }

But this function doesn't take into account 2 (and probably more) cases: if the sub-array are not in the same order, if a sub-array or a value only exist in second array.

A example of function would be a good help, but on a more fundamental level, I even can't figure the algorithm to do that. Any ideas ?

  • This may helps, [How to merge two arrays by summing the merged values](https://stackoverflow.com/a/6086409/6521116) – LF00 Jun 17 '17 at 17:50

1 Answers1

0

You can get all the keys for the foreach loop, live demo.

Note, you also can check if a key of any array is undefined, then save the defined value for the key.

function sumArrayValues($array1, $array2)
{
    $keys = array_keys($array1 + $array2);
    foreach ($keys as $key) 
    {
        if (is_array($array1[$key]) || is_array($array2[$key]))
            $array1[$key] = sumArrayValues($array1[$key], $array2[$key]);
        else
            @$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
    }
    return $array1;
}
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    Your example works great, except when $arrayX[$key] is NULL, so I had to add a condition at the beginning of the function that creates arrays when they don't exist. – remi legend Jun 18 '17 at 20:16