Multidimensional array in php SESSION

前端 未结 2 809
北恋
北恋 2020-12-08 18:12

I am having problem with updating an array element with in $_SESSION variable of PHP. This is the basic structure:

$product = array();
$product[         


        
2条回答
  •  Happy的楠姐
    2020-12-08 18:31

    Don't blindly stuff the product into your session. Use the product's ID as the key, then it's trivial to find/manipulate that item in the cart:

    $_SESSION['cart'] = array();
    $_SESSION['cart'][$id] = array('type' => 'foo', 'quantity' => 42);
    
    $_SESSION['cart'][$id]['quantity']++; // another of this item to the cart
    unset($_SESSION['cart'][$id]); //remove the item from the cart
    

提交回复
热议问题