我有一张表格,如下图所示,但我希望对AMOUNT REMAINING
进行求和,并合并重复项,如SHIPPED COMPLETE NOT EQUAL COMPLETE
或AMOUNT REMAINING NOT EQUAL to 0
。
我希望将新表视为
Part Number Quantity Used In WO
400443 8
400571 8
400458 8
400441 28 // 14+14
400497 28 // 14+14
200391 28 // 14+14
201937 28 // 14+14
201804 6
201283 9
400629 1
我尝试了下面的代码,但我不能总结并合并重复项并将不同的记录插入到Mysql表中
$stmt = mysqli_prepare($con, "
INSERT INTO demand (PartNo,qty) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, "si", $itemid, $qty) or die(mysqli_error($con));
$recordd = $tv->search(56554000270957, 's=2');
foreach($recordd as $data2) {
$itemrim = $data2['fields']['RIM PN (MRP)'];
$itemtire = $data2['fields']['TIREPN'];
$qty = $data2['fields']['QUANTITY'];
if ($data2['fields']['AMOUNT REMAINING)']!=0 || $data2['fields']['SHIPPED COMPLETE)'] !="COMPLETE") {
//Sum (value of $data2['fields']['QUANTITY'];) of duplicate item
mysqli_stmt_execute($stmt) or die(mysqli_stmt_error($stmt));
}
// print_r的
Array
(
[0] => Array
(
[id] => 99994000270957
[table_id] => 56554000270957
[created] => 2014-09-08T18:53:03-0600
[updated] => 2016-04-26T12:37:12-0600
[created_by] => 3000045174
[updated_by] => 3000045538
[fields] => Array
([MRP Wheel] => 500356
[TIRE PN] => 200248
[RIM PN (MRP)] => 400479
)
)
答案 0 :(得分:0)
您可以更改查询以获取记录使用sql sum()
添加金额
select RIMPN(MRP),TIREPN, sum(AMOUNT_REMAINING) as QUANTITY from table_name group by RIMPN(MRP)
创建新数组的另一种方法..
<?php
$recordd = array(0=>array('id'=>99994000270957,'fields'=>array('RIM PN (MRP)'=>'400441','TIREPN'=>200391,'QUANTITY'=>14)),
1=>array('id'=>99994000270958,'fields'=>array('RIM PN (MRP)'=>'400441','TIREPN'=>200391,'QUANTITY'=>14)),
2=>array('id'=>99994000270959,'fields'=>array('RIM PN (MRP)'=>'400497','TIREPN'=>201937,'QUANTITY'=>14))
);
$newArray = array();
foreach($recordd as $data2)
{
$newArray[$data2['fields']['RIM PN (MRP)']][] = $data2;
}
$stmt = mysqli_prepare($con, "INSERT INTO demand (PartNo,qty) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, "si", $itemid, $qty) or die(mysqli_error($con));
foreach($newArray as $key => $val)
{
$itemid = $val[0]['fields']['RIM PN (MRP)'];
$qty = array_sum(array_map(function ($ar) {return $ar['fields']['QUANTITY'];}, $val));
mysqli_stmt_execute($stmt) or die(mysqli_stmt_error($stmt));
}
?>