简体   繁体   中英

PHP on find the array with two keys equal to two values ​in the array and remove it from the array

I'm developing a shopping cart. I keep the basket in one session. I can add the product to the cart. But I don't know how to remove product line from basket session.

Session structure; The session holds an array and the product arrays in it. I want to access the array I want to remove with two keys.

//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 ?



First since your prodID should be unique in the session you can set it as the basket key

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM