I have the following code. i want the elseif part to check if $pId is already in the array and if it is i want to increase its quantity and price rather than adding a new $pId to the array.
I don’t know if i am using the wrong method or if my array structure is wrong but i cant get it to increase those values
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;
}
I’ve added an extra dimension to the array so you could easily select it.
I don’t know what
pidstands for, but at first glance it doesn’t really look descriptive. Maybeproductswould be a better key?