php多维数组重复求和问题

发布于 2022-09-05 15:45:29 字数 1131 浏览 32 评论 0

[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分别是总和的数组。这个怎么写。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2022-09-12 15:45:31

点击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);
悲歌长辞 2022-09-12 15:45:31

我先假设你题设中给的 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);
梅窗月明清似水 2022-09-12 15:45:31

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

<?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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文