doufan3958
2019-06-20 10:02
浏览 304
已采纳

多维数组中的求和值

I want to sum the values of the following array I would like to group the same items by cumulating them, the output format would also be an array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => R421_FD03
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => R421_FD03
                    [1] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => R421_FD03
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => R421_FD02
                    [1] => 1
                )

        )

)

I tested this code but the result is not the one I'm waiting for:

$sumArray = array();

        array_walk_recursive($data, function($item, $key) use (&$sumArray){
            $sumArray[$key] = isset($sumArray[$key]) ?  $item + $sumArray[$key] : $item;
        });

The result I'm waiting for is this one

Array
(
    [0] => Array
        (
            [0] => R421_FD03
            [1] => 3
        )

    [1] => Array
        (
            [0] => R421_FD02
            [1] => 1
        )

     )
)

图片转代码服务由CSDN问答提供 功能建议

我想总结以下数组的值 我想通过累积它们来对相同的项进行分组, 输出格式也是一个数组

  Array 
(
 [0] =>数组
(
 [0] =>数组
(\  n [0] => R421_FD03 
 [1] => 1 
)
 
 [1] =>数组
(
 [0] => R421_FD03 
 [1] =>  ; 1 
)
 
)
 
 [1] =>数组
(
 [0] =>数组
(
 [0] => R421_FD03 
 [1]  => 1 
)
 
 [1] =>数组
(
 [0] => R421_FD02 
 [1] => 1 
)
 
)
 
 \  n)
   
 
 

我测试了这段代码但结果不是我正在等待的那个:

   $苏马尔 ray = array(); 
 
 array_walk_recursive($ data,function($ item,$ key)use(& $ sumArray){
 $ sumArray [$ key] = isset($ sumArray [$ key])?  $ item + $ sumArray [$ key]:$ item; 
}); 
   
 
 

我正在等待的结果是这一个 \ n

  Array 
(
 [0] =>数组
(
 [0] => R421_FD03 
 [1] => 3 
)
 
  [1] =>数组
(
 [0] => R421_FD02 
 [1] => 1 
)
 
)
)
   \  n 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongping5230 2019-06-20 10:09
    已采纳

    You can apply simple foreach() with array_values()

    $final_array = [];
    
    foreach($array as $arr){
        foreach($arr as $ar){
            $final_array[$ar[0]][0] = $ar[0];
            $final_array[$ar[0]][1] = (isset($final_array[$ar[0]][1])) ?  $final_array[$ar[0]][1] + $ar[1] : $ar[1];
        }
    
    }
    
    $final_array = array_values($final_array);
    print_r($final_array);
    

    Output:-https://3v4l.org/lABWG

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujin8673 2019-06-20 12:50

    You can use array_merge with splat operator and then iterate through the array

    $merged = array_merge(...$arr);//splat operator
    $r = [];
    array_walk($merged, function($v,$k) use(&$r){
     array_key_exists($v['0'], $r) ? ($r[$v[0]] += $v['1']) : ($r[$v[0]] = $v[1]);
    });
    print_r($r);
    

    Result :-

    Array
    (
     [R421_FD03] => 3
     [R421_FD02] => 1
    )
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题