0
我有以下代碼。我想elseif
部分檢查$pId
是否已經在陣列中,如果是我想增加其quantity
和price
,而不是將新的$pId
添加到陣列。如何檢查數組中是否存在某個值,以及它是否增加該值
我不知道我是否使用了錯誤的方法,或者如果我的陣列結構是錯誤的,但我不能得到它來增加這些值
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
$_SESSION['cart']['pid'][] = $pid;
$_SESSION['cart']['pid']['price'] = $price;
$_SESSION['cart']['pid']['quantity'] = $quantity;
$_SESSION['cart']['total_price'] = $price;
$_SESSION['cart']['total_items'] = $quantity;
}elseif(array_key_exists($pid, $_SESSION['cart'])){
//increase price
//increase quantity
}else{
$_SESSION['cart']['pid'][] = $pid;
$_SESSION['cart']['pid']['price'] = $price;
$_SESSION['cart']['total_price'] += $price;
$_SESSION['cart']['total_items'] += $quantity;
}
增加什麼? – PeeHaa 2012-04-25 22:48:03
@RepWhoringPeeHaa當前值加上新值。例如'價格+ ='; – code511788465541441 2012-04-25 22:49:38