为了账号安全,请及时绑定邮箱和手机立即绑定

php多维数组重复求和问题

php多维数组重复求和问题

PHP
弑天下 2019-03-17 00:27:25
[10] => array(6) { ["barcode"] => string(12) "041167660317" ["qty"] => int(1) ["price"] => string(5) "55.80" ["tax"] => string(18) "6.6401800000000000" } [11] => array(6) { ["barcode"] => string(13) "9311770598279" ["qty"] => int(1) ["price"] => string(5) "97.00" ["tax"] => string(19) "11.5426800000000000" } [12] => array(6) { ["barcode"] => string(13) "9349254002288" ["qty"] => int(2) ["price"] => string(5) "55.10" ["tax"] => string(18) "6.5572300000000000" } [13] => array(6) { ["barcode"] => string(13) "9311770598279" ["qty"] => int(2) ["price"] => string(5) "83.00" ["tax"] => string(18) "9.8767900000000000" } [14] => array(6) { ["barcode"] => string(13) "9311770598279" ["qty"] => int(1) ["price"] => string(5) "97.00" ["tax"] => string(19) "11.5426800000000000" } 我想把barcode值一样的数组price、tax和qty分别求和。生成新的组。就是只要是barcode一样,price、qty和tax就分别相加。最后生成没有重复的但price、tax和qty分别是总和的数组。这个怎么写。
查看完整描述

3 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

点击run获取:执行结果

<?php
$arr = array(
    array( 
        "barcode" => "041167660317",   
        "qty" => 1,   
        "price" => "55.80",
        "tax" => "6.6401800000000000"
    ),
    array(
        "barcode" => "9311770598279",
        "qty" => 1,    
        "price" => "97.00",
        "tax" => "11.5426800000000000"
    ),
    array(
        "barcode" => "9349254002288", 
        "qty" => 2,   
        "price" => "55.10",
        "tax" => "6.5572300000000000"
    ),
    array(
        "barcode" => "9311770598279",
        "qty" => 2,   
        "price" => "83.00",
        "tax" => "9.8767900000000000"
    ),
    array( 
      "barcode" => "9311770598279",
      "qty" => 1,   
      "price" => "97.00",
      "tax" => "11.5426800000000000"
    )
);


$unique_keys = array();
foreach ($arr as $key => $value) {
   if (isset($unique_keys[$value["barcode"]])) {
        $index = $unique_keys[$value["barcode"]];
        foreach ($arr[$index] as $k => &$v) {
            if ($k !== "barcode") {
                $v += $value[$k];
            }
        }
        unset($arr[$key]);
    } else {
        $unique_keys[$value["barcode"]] = $key;
    }
}
var_dump($arr);
查看完整回答
反对 回复 2019-03-18
?
青春有我

TA贡献1784条经验 获得超8个赞

我先假设你题设中给的 key 是数组是错误的,key我就当做是个字符串。

其实用 foreach 一样可以。

$a 就表示你给出的数组

$result = [];
array_walk($a, function ($item) use (&$result) {
    $barcode = $item['barcode'];
    if (isset($result[$barcode])) {
        $tmp = $result[$barcode];
        $tmp['price'] = bcadd($tmp['price'], $item['price'], 2);
        $tmp['qty'] += $item['qty'];
        $tmp['tax'] = bcadd($tmp['tax'], $item['tax'], 5);

        $result[$barcode] = $tmp;
    } else {
        $result[$barcode] = $item;
    }
});

$result = array_values($result);
查看完整回答
反对 回复 2019-03-18
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

假定原数据可以被破坏,可以这样节省一点内存。

<?php
$res = array();
$arr = array(
  array( 
    "barcode" => "041167660317",   
    "qty" => 1,   
    "price" => "55.80",
    "tax" => "6.6401800000000000"
  ),
  array(
    "barcode" => "9311770598279",
    "qty" => 1,    
    "price" => "97.00",
    "tax" => "11.5426800000000000"
  ),
  array(
    "barcode" => "9349254002288", 
    "qty" => 2,   
    "price" => "55.10",
    "tax" => "6.5572300000000000"
  ),
  array(
    "barcode" => "9311770598279",
    "qty" => 2,   
    "price" => "83.00",
    "tax" => "9.8767900000000000"
  ),
  array(
    "barcode" => "9311770598279",
    "qty" => 1,   
    "price" => "97.00",
    "tax" => "11.5426800000000000"
  )
);

foreach ($arr as &$val) {
  $code = $val['barcode'];
  if (isset($res[$code])){
    $code = &$res[$code];
    $code['qty'] += $val['qty'];
    $code['tax'] += $val['tax'];
    $code['price'] += $val['price'];
  }
  else {
    $res[$code] = &$val;
  }
  unset($val, $code);
}

var_dump($res);
查看完整回答
反对 回复 2019-03-18
  • 3 回答
  • 0 关注
  • 628 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信