合并2数组和值相加(数字键) [英] Merge 2 Arrays and Sum the Values (Numeric Keys)

查看:121
本文介绍了合并2数组和值相加(数字键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个阵列

 阵列

    [010156] =&放大器; GT;排列
    (
        [supp_number] =&放大器; GT; 010156
        [总金额] =&放大器; GT; 4.113,23
        [debtorcred] =&放大器; GT; H
        [amount1] =&放大器; GT; 4.113,23-
        [AMOUNT2] =&放大器; GT;        [AMOUNT3] =&放大器; GT;
        [amount4] =&放大器; GT;
        [amount5] =&放大器; GT;
        [amount6​​] =&放大器; GT;
    )
)排列

    [010156] =&放大器; GT;排列
    (
        [supp_number] =&放大器; GT; 010156
        [总金额] =&放大器; GT; 4.113,23
        [debtorcred] =&放大器; GT; H
        [amount1] =&放大器; GT; 4.113,23-
        [AMOUNT2] =&放大器; GT;        [AMOUNT3] =&放大器; GT;
        [amount4] =&放大器; GT;
        [amount5] =&放大器; GT;
        [amount6​​] =&放大器; GT;
    )

难道我可以将这些不同的阵列组合成一个,并添加值在一起,这样的结果将是:

 阵列

    [010156] =&放大器; GT;排列
    (
        [supp_number] =&放大器; GT; <<<总计>>>
        [总金额] =&放大器; GT; <<<总计>>>
        [debtorcred] =&放大器; GT; <<<总计>>>
        [amount1] =&放大器; GT; <<<全>>>
        [AMOUNT2] =&放大器; GT; <<<总计>>>        [AMOUNT3] =&放大器; GT; <<<总计>>>
        [amount4] =&放大器; GT; <<<总计>>>
        [amount5] =&放大器; GT; <<<总计>>>
        [amount6​​] =&放大器; GT; <<<总计>>>
    )

这是我此刻的功能,但我似乎无法使其工作:

 函数array_merge_numeric_values​​()
{
  $数组= func_get_args();
  $合并=阵列();
  的foreach($数组作为$数组)
  {
    的foreach($数组$关键=> $值)
    {
      如果(!使用isset($合并[$关键]))
      {
        $合并[$关键] = $价值;
      }
      其他
      {
        $合并[$键] + = $价值;
      }
    }
  }


解决方案

这实际计算可以在2线code来完成,无需循环:

HTTP://$c$cpad.viper-7.com/ieSkHQ

  $ ARR1 =阵列('amount1'=大于1,'AMOUNT2'=> 6);
$ ARR2 =阵列('amount1'=大于2,'AMOUNT2'=大于7);
$ =添加功能($ A,$ B){返回$ A + $ B; };
$ summedArray = array_map($添加,$ ARR1,$ ARR2);
的print_r($ summedArray);

Youl'll只需要进行相应的调整来解释你嵌套结构。

I have 2 arrays

Array
(
    [010156] => Array
    (
        [supp_number] => 010156
        [totalamount] =>     4.113,23
        [debtorcred] => H
        [amount1] =>     4.113,23-
        [amount2] =>

        [amount3] =>
        [amount4] =>
        [amount5] =>
        [amount6] =>
    )
)



Array
(
    [010156] => Array
    (
        [supp_number] => 010156
        [totalamount] =>     4.113,23
        [debtorcred] => H
        [amount1] =>     4.113,23-
        [amount2] =>

        [amount3] =>
        [amount4] =>
        [amount5] =>
        [amount6] =>
    )
)

Is it possible that I can combine these separate arrays into one and add the values together so that the outcome will be:

Array
(
    [010156] => Array
    (
        [supp_number] =&gt;  <<<TOTAL >>>
        [totalamount] =&gt;     <<<TOTAL >>>
        [debtorcred] =&gt; <<<TOTAL >>>
        [amount1] =&gt;      <<<TOTAL >>>
        [amount2] =&gt;  <<<TOTAL >>>

        [amount3] =&gt;  <<<TOTAL >>>
        [amount4] =&gt;  <<<TOTAL >>>
        [amount5] =&gt;  <<<TOTAL >>>
        [amount6] =&gt;  <<<TOTAL >>>
    )
)

This is the function I have at the moment but I cannot seem to make it work:

function array_merge_numeric_values()
{
  $arrays = func_get_args();
  $merged = array();
  foreach ($arrays as $array)
  {
    foreach ($array as $key => $value)
    {
      if ( ! isset($merged[$key]))
      {
        $merged[$key] = $value;
      }
      else
      {
        $merged[$key] += $value;
      }
    }
  }

解决方案

This actual calculation can be done in 2 lines of code, no need for loops:

http://codepad.viper-7.com/ieSkHQ

$arr1 = array('amount1' => 1, 'amount2' => 6);
$arr2 = array('amount1' => 2, 'amount2' => 7);
$add = function($a, $b) { return $a + $b; };
$summedArray = array_map($add, $arr1, $arr2);
print_r($summedArray);

Youl'll just need to make the appropriate adjustment to account for you nested structure.

这篇关于合并2数组和值相加(数字键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆