dongying7667
2018-01-02 09:12
浏览 45
已采纳

从多维数组中求和并合并重复值

I would like to sum the value of movements while keeping an unique value for day on the same array.

The base array:

array (607)
0 => array (2)
    day => "11" (2)
    movements => 1
1 => array (2)
    day => "11" (2)
    movements => 1
2 => array (2)
    day => "11" (2)
    movements => 1
3 => array (2)
    day => "12" (2)
    movements => 1
4 => array (2)
    day => "12" (2)
    movements => 1
5 => array (2)
    day => "12" (2)
    movements => 1

[...]

The result I am expecting (for each day sum movements):

array (607)
    0 => array (2)
        day => "11" (2)
        movements => 3
    1 => array (2)
        day => "12" (2)
        movements => 3    
    [...]

Any help would be greatly appreciated.

Edit: What I have tried so far :

  • sum-array-values-of-the-same-key: it also sum daywhich is not what I am expecting.
  • my own code:

    $arr1 = $stats['data']['vhc']; // contain ['day'] and ['movements']
    $arr2 = $data['vhc']; // contain ['day'] and ['movements']
    $sum = [];
    
    if (!empty($arr1)) {
        $result = [];
        foreach ($arr1 as $key => $value) {
            if (isset($arr2[$key]['day']) && $arr2[$key]['day'] == $value['day']) {
                $result['day'] = $value['day'];
                $result['movements'] = $value['movements'] + $arr2[$key]['movements'];
            }
            array_push($sum, $result);
        }
    }
    
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douji5397 2018-01-02 09:32
    已采纳

    You can easily do with foreach() and array_values()

    $final_array = [];
    
    foreach($array as $arr){
      $final_array[$arr['day']]['day'] = $arr['day'];
      $final_array[$arr['day']]['movements'] = (isset($final_array[$arr['day']]['movements']))? $final_array[$arr['day']]['movements']+$arr['movements'] : $arr['movements'];    
    }
    $final_array = array_values($final_array);
    
    print_r($final_array);
    

    Output:-https://eval.in/957326

    已采纳该答案
    打赏 评论
  • dthp96899 2018-02-16 17:47

    This method will not bother overwriting result array data with the same value.

    Code: (Demo)

    $array=[
        ['day'=>'11','movements'=>'1'],
        ['day'=>'11','movements'=>'1'],
        ['day'=>'11','movements'=>'1'],
        ['day'=>'12','movements'=>'1'],
        ['day'=>'12','movements'=>'1'],
        ['day'=>'12','movements'=>'1']
    ];
    foreach($array as $row){  // iterate all rows
        if(!isset($result[$row['day']])){  // if first occurrence of day...
            $result[$row['day']]=$row;     // save the full row with day as the temporary key
        }else{                                    // if not the first occurrence of day...
            $result[$row['day']]['movements']+=$row['movements'];  // add movements value
        }
    }
    var_export(array_values($result));
    

    Output:

    array (
      0 => 
      array (
        'day' => '11',
        'movements' => 3,
      ),
      1 => 
      array (
        'day' => '12',
        'movements' => 3,
      ),
    )
    
    打赏 评论

相关推荐 更多相似问题