php根据二维数组中的某一元素相等,另一个元素相加

二维数组:$taskData
 
Array
(
    [0] => Array
        (
            [area] => 1
            [winsFlag] => 7
            [count] => 3
        )
 
    [1] => Array
        (
            [area] => 1
            [winsFlag] => 3
            [count] => 2
        )
 
    [2] => Array
        (
            [area] => 1
            [winsFlag] => 5
            [count] => 2
        )
 
    [3] => Array
        (
            [area] => 2
            [winsFlag] => 6
            [count] => 2
        )
 
    [4] => Array
        (
            [area] => 2
            [winsFlag] => 3
            [count] => 2
        )
 
    [5] => Array
        (
            [area] => 3
            [winsFlag] => 7
            [count] => 3
        )
 
    [6] => Array
        (
            [area] => 3
            [winsFlag] => 1
            [count] => 1
        )
 
    [7] => Array
        (
            [area] => 4
            [winsFlag] => 2
            [count] => 1
        )
 
    [8] => Array
        (
            [area] => 4
            [winsFlag] => 2
            [count] => 1
        )
 
)

根据area相同将count相加

$item=[];
foreach($taskData as $k=>$v){
    if(!isset($item[$v['area']])){
        $item[$v['area']] = $v;
    }else{
        $item[$v['area']]['count'] += $v['count'];
    }
}

结果:得到一个新的数组 $item

Array
(
    [1] => Array
        (
            [area] => 1
            [winsFlag] => 7
            [count] => 7
        )
 
    [2] => Array
        (
            [area] => 2
            [winsFlag] => 6
            [count] => 4
        )
 
    [3] => Array
        (
            [area] => 3
            [winsFlag] => 7
            [count] => 4
        )
 
    [4] => Array
        (
            [area] => 4
            [winsFlag] => 2
            [count] => 2
        )
 
)

根据需求改变一下,把相同的值取出来,其余拼接一起

$item=[];
foreach($equityInfos as $k=>$v){
    if(!isset($item[$v['uid']])){
        $item[$v['uid']] = $v;
    }else{
        $item[$v['uid']]['product_id'] .= ','. $v['product_id'];
    }
}

  上面说的是根据一个元素相等,假如要根据两个元素相等呢,下面就来解决

Array
(
    [0] => Array
        (
            [send_detail_id] => 65379
            [amount] => 1
            [sn] => MB12-69-181
        ),
    [1] => Array
        (
            [send_detail_id] => 65380
            [amount] => 2
            [sn] =>MB12-69-181
        ),
    [2] => Array
        (
            [send_detail_id] =>65380
            [amount] => 1
            [sn] => MB12-69-181
        ),
    [3] => Array
        (
            [send_detail_id] => 65382
            [amount] => 1
            [sn] => SA12-64-180N2001
        )
)
如果两个字段send_detail_id和sn相同才合并求和,结果应该是这种
Array
(
    [0] => Array
        (
            [send_detail_id] => 65379
            [amount] => 1
            [sn] => MB12-69-181
        ),
    [1] => Array
        (
            [send_detail_id] => 65380
            [amount] => 3  //和后面的相加了,第一个就不能和他相加
            [sn] =>MB12-69-181
        ),
  
    [3] => Array
        (
            [send_detail_id] => 65382
            [amount] => 1
            [sn] => SA12-64-180N2001
        )
)
其实很简单
foreach($arr as $v){
    if(!isset($ret[$v['send_detail_id'].'_'.$v['sn']])){
        $ret[$v['send_detail_id'].'_'.$v['sn']] = $v;
    }else{
        $ret[$v['send_detail_id'].'_'.$v['sn']]['amount'] += $v['amount'];
    }
}
print_r(array_values($ret));

 

posted @ 2019-05-04 22:06  御世制人  阅读(1147)  评论(0编辑  收藏  举报