答案 0 :(得分:1)
请查看以下代码,它可能对您有帮助。
$values = array (array ("item_id" =>13 ,"item_qty" => 3 ) , array ( "item_id" => 10 , "item_qty" => 7 ) , array ( "item_id" => 13 , "item_qty"=> 2 ) , array ( "item_id" => 10 , "item_qty" => 4 ) , array ( "item_id" => 10 , "item_qty" => 4 ) );
$new_array = array();
foreach($values as $single)
{
if(array_key_exists($single['item_id'],$new_array))
{
$new_array[$single['item_id']] += $single['item_qty'];
}
else
{
$new_array[$single['item_id']] = $single['item_qty'];
}
}
print_r($new_array);
这将为您提供类似
的结果Array ( [13] => 5 [10] => 15 )
项目ID 13 =数量5和项目ID 10 =数量15
答案 1 :(得分:0)
将ID添加到数组中,然后递增QTY:
foreach($arr as $Item){
$ItemID = $Item['item_id'];
$ItemQTY = $Item['item_qty'];
if(!isset($Result[$ItemID])){
$Result[$ItemID] = 0;
}
$Result[$ItemID] += $ItemQTY;
}
print_r($Result);