时间:2021-07-01 10:21:17 帮助过:26人阅读
$cart = array(1=>array("id"=>1,"num"=>2),2=>array("id"=>2,"num"=>5));
$newcart = array(1=>array("id"=>1,"num"=>4),3=>array("id"=>3,"num"=>4));
foreach( $newcart as $k=>$v ){
if( $cart[$v['id']] ){
$cart[$v['id']]['num'] += $v['num'];
}
else{
$cart = array_merge($cart,array($v));
}
}
$cart = array(array("id"=>1,"num"=>2));
$newcart = array(array("id"=>1,"num"=>4),array("id"=>2,"num"=>4));
foreach($cart as $v) $t[$v['id']]+=$v['num'];
foreach($newcart as &$v){
$v['num']+=$t[$v['id']];
}
$cart =$newcart;
print_r($cart);