php多维数组重复求和问题

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

回复
阅读 2.8k
3 个回答

点击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);

我先假设你题设中给的 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);

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

<?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);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏