Sum the values of two associative multi-dimensional arrays in PHP

后端 未结 3 454
失恋的感觉
失恋的感觉 2020-12-21 18:36

I am trying to sum the values of two associative arrays. This is the first array:

Array
(
[Jan 01, 2013] => Array
    (
        [COM] => 100
        [         


        
相关标签:
3条回答
  • 2020-12-21 19:18

    Use foreach() for first array & check the key of first array whether it exist or not in second array. If exist the calculate sum. Example:

    $arr1 = Array
    (
        "Jan 01, 2013" => Array
        (
            "COM" => 100,
            "RES" => 200,
        ),
    
        "Oct 28, 2014" => Array
        (
            "COM" => 300,
            "RES" => 400,
        )
    );
    $arr2 = Array
    (
        "Jan 01, 2013" => Array
        (
            "COM" => 10,
            "RES" => 20,
        ),
    
        "Oct 28, 2014" => Array
        (
            "COM" => 30,
            "RES" => 40,
       )
    );
    
    $arr3 = array();
    foreach($arr1 as $key => $val):
        if(array_key_exists($key, $arr2)):
            $arr3[$key]["COM"] = $val["COM"] + $arr2[$key]["COM"];
            $arr3[$key]["RES"] = $val["RES"] + $arr2[$key]["RES"];
        endif;
    endforeach;
    
    print '<pre>';
    print_r($arr3);
    print '</pre>';
    

    Output:

    Array
    (
        [Jan 01, 2013] => Array
            (
                [COM] => 110
                [RES] => 220
            )
    
        [Oct 28, 2014] => Array
            (
                [COM] => 330
                [RES] => 440
            )
    
    )
    
    0 讨论(0)
  • 2020-12-21 19:21

    Most directly/simply, iterate the levels of your second array and determine if the values should be appended to the first array, or merge-summed into it.

    Code: (Demo)

    $arr1 = [
        "Jan 01, 2013" => ["COM" => 100, "RES" => 200, "FOO" => 5],
        "Oct 28, 2014" => ["COM" => 300, "RES" => 400]
    ];
    $arr2 = [
        "Jan 01, 2013" => ["COM" => 10, "RES" => 20],
        "Oct 28, 2014" => ["COM" => 30, "RES" => 40]
    ];
    
    foreach ($arr2 as $date => $set) {
        foreach ($set as $key => $val) {
            if (!isset($arr1[$date][$key])) {            // val is unique to 1st array, set it
                $arr1[$date][$key] = $val;
            } else {                // val already exists in 1st array, add 2nd val to 1st val
                $arr1[$date][$key] += $val;
            }
        }
    }
    var_export($arr1);
    

    Because you don't know the subarray keys for your incoming data and you have associative keys, you can use array_merge_recursive() to merge your two arrays in a way that sets up very simple summing.

    After merging based on associative values, you can loop the new multi-dimensional array. While iterating, signify the subarray and the subarray's values as "modifiable by reference" -- this means that the foreach loops will process the actual array rather than a copy of the array. Effectively, this will make the changes affect the new array and deliver the desired result via $assoc_merged.

    Code: (Demo)

    $arr1 = [
        "Jan 01, 2013" => ["COM" => 100, "RES" => 200, "FOO" => 5],
        "Oct 28, 2014" => ["COM" => 300, "RES" => 400]
    ];
    $arr2 = [
        "Jan 01, 2013" => ["COM" => 10, "RES" => 20],
        "Oct 28, 2014" => ["COM" => 30, "RES" => 40]
    ];
    
    $assoc_merged =array_merge_recursive($arr1, $arr2);
    // var_export($assoc_merged);  // see what this makes
    
    foreach ($assoc_merged as $date => &$set) {
        foreach ($set as $key => &$val) {
            $val = array_sum((array)$val);
            // (array) forces a lone value to be an array containing one element; avoids generating Warnings
        }
    }
    var_export($assoc_merged);
    

    Both approaches produce the same output:

    array (
      'Jan 01, 2013' => 
      array (
        'COM' => 110,
        'RES' => 220,
        'FOO' => 5,
      ),
      'Oct 28, 2014' => 
      array (
        'COM' => 330,
        'RES' => 440,
      ),
    )
    
    0 讨论(0)
  • 2020-12-21 19:22

    Try this:

    <?php
       $array_01 = array(
          'a' => 1,
          'b' => 2
       );
    
        $array_02 = array(
          'a' => 0,
          'b' => 1,
          'c' => 2
        );
    
        /** first we should get the array keys union
         * this is too long...,so you can make it better :D
         * you can make this more readable
         */
        $keyUnion = array_unique(array_merge(array_keys($array_01), array_keys($array_02))); 
        $res = array();
    
        //sum
        foreach ($keyUnion as $k => $v) {
            $res[$v] = (isset($array_01[$v]) ? $array_01[$v] : 0) + (isset($array_02[$v]) ? $array_02[$v] : 0);
        }
    
        print_r($res);
    ?>
    

    Notice: code only get the one-dimensional associative sum

    0 讨论(0)
提交回复
热议问题