问题描述
我正在开发购物车。我把篮子放在一堂课上。我可以将产品添加到购物车中。但是我不知道如何从购物篮会话中删除产品线。@H_404_1@
会话结构;会话中包含一个数组和一个产品数组。我想用两个键访问要删除的数组。@H_404_1@
//Create basket session
$_SESSION['basket'] = [];
//Add a Product
$productArray = ['prodID' => '1','quantity' => '3'];
$_SESSION['basket'][] = $productArray;
//Post method comes with two variables (keys).
$prodID = $_POST['prodID'];
$quantity = $_POST['quantity'];
//How can I remove the array with keys equal to $prodID and $quantity within the session ?
解决方法
首先,由于您的prodID在会话中应该是唯一的,因此可以将其设置为basket
键
//Create basket session
$_SESSION['basket'] = [];
//Add a Product
$productArray = ['prodID' => '1','quantity' => '3'];
$_SESSION['basket'][$productArray['prodID']] = $productArray;
//Post method comes with two variables (keys).
$prodID = $_POST['prodID'];
$quantity = $_POST['quantity'];
//How can I remove the array with keys equal to $prodID and $quantity within the session ?
//Now you only need to update / replace the quantity here.