我有以下代码。我希望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;
}