-4

我是php新手。我需要两个数组的总和,但没有得到正确的输出。我的数组是以这种方式出现的。

这是我的第一个数组。

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [-10] => 21787048.7293
                    [-5] => 21816115.9548
                    [-1] => 21839369.7352
                    [0] => 21845183.1803
                    [1] => 21850996.6254
                    [5] => 21874250.4058
                    [10] => 21903317.6313
                )

        )

    [2] => Array
        (
            [2] => Array
                (
                    [-10] => 21147607.6407
                    [-5] => 21496395.4105
                    [-1] => 21775425.6263
                    [0] => 21845183.1803
                    [1] => 21914940.7343
                    [5] => 22193970.9501
                    [10] => 22542758.7199
                )

        )

)

这是第二个数组。

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [-10] => 26101989.9443
                    [-5] => 26131057.1698
                    [-1] => 26154310.9501
                    [0] => 26160124.3952
                    [1] => 26165937.8403
                    [5] => 26189191.6207
                    [10] => 26218258.8462
                )

        )

    [2] => Array
        (
            [2] => Array
                (
                    [-10] => 25462548.8556
                    [-5] => 25811336.6254
                    [-1] => 26090366.8413
                    [0] => 26160124.3952
                    [1] => 26229881.9492
                    [5] => 26508912.1651
                    [10] => 26857699.9349
                )

        )

)

我需要按键对这两个数组求和。请帮帮我。

这是我的代码。我的两个数组是 $pretax_income,$earning。

foreach($pretax_income as $k=>$value)
        {
          foreach($value as $v=>$val)
          {
            foreach($val as $u=>$valArr) {
            $comboarray[$elno] = $valArr[$k][$v][$u] + $earning[$k][$v][$u];

            }   
          } 


        }   print_r($comboarray);
4

2 回答 2

0
function sum()
{
    $identicalKeysArrays = func_get_args();

    if (is_array($identicalKeysArrays[0])) {
        $result = call_user_func_array('sum', array_map(function ($array) {
            return $array[0];
        }, $identicalKeysArrays));

        if ($result) {
            return $result;
        }
    }

    if ($identicalKeysArrays[0]) {
        $summed = array();
        foreach ($identicalKeysArrays as $array) {
            foreach ($array as $key => $value) {
                $summed[$key] = ($summed[$key] ? $summed[$key] : 0) + $value;
            }

        }

        return $summed;
    }
}

$a = array(array(
    '-1' => 23,
    '2'  => 2
));

$b = array(array(
    '-1' => 3
));

var_dump(sum($a, $b));

结果:array(2) { [-1]=> int(26) [2]=> int(2) }

于 2013-10-18T13:32:29.927 回答
-1

也许是这样的?

foreach($pretax_income as $key1=>$val1){
  foreach($val1 as $key2=>$val2){
    foreach($val2 as $key3=>$val3){
      $comboarray[$key1][$key2][$key3]=$pretax_income[$key1][$key2][$key3]+$earning[$key1][$key2][$key3];
    }
  }
}
于 2013-10-18T12:55:37.473 回答