简体   繁体   English

如何从用作会话购物车的数组中删除商品ID

[英]How to delete item ID from array used as session shopping cart

I have a SESSION['cart'] with ID numbers only. 我有一个仅具有ID号的SESSION ['cart']。 I have a form passing the ID with a remove button. 我有一个表格,传递带有删除按钮的ID。 Once passed to my controller, I cannot figure out how to write the code that uses the ID ($_POST['id']) to delete the item from the SESSION['cart']. 一旦传递给我的控制器,我就无法弄清楚如何编写使用ID($ _POST ['id'])从SESSION ['cart']中删除项目的代码。

I can loop through and display the array contents, but I cannot figure out how to delete based on ID passed from the form. 我可以遍历并显示数组内容,但是无法根据从表单传递的ID弄清楚如何删除。

How do I loop through the SESSION['cart'] array to find a match with the ID passed from my delete form, and then delete that ID? 如何遍历SESSION ['cart']数组以查找与从删除表单传递的ID匹配的内容,然后删除该ID? I know that unset($_SESSION['cart'][X] deletes the ID at index X, but I cannot figure out how to loop through all the elements to find a match. 我知道unset($ _ SESSION ['cart'] [X]删除索引X处的ID,但是我不知道如何遍历所有元素以找到匹配项。

I have read a number of related issues in this forum but have been unable to apply any of those solutions to resolve this challenge. 我已经在该论坛上阅读了许多相关问题,但是无法应用任何解决方案来解决这一挑战。 Any assistance is appreciated. 任何帮助表示赞赏。

The way you have your values ( $products = array(3,7,99,152) ) isn't a very good method. 您拥有值的方式( $products = array(3,7,99,152) )不是一个很好的方法。 Every time you want to perform an action, you have to loop through the array, you don't want that . 每次要执行操作时,都必须遍历数组, 而不是那样 Apart from that, how do you store quantity? 除此之外,您如何存储数量? Or variations like eg size or color? 还是诸如大小或颜色之类的变化?

if your structure is $array[ ID_OF_PRODUCT ] , you can simply do this: 如果您的结构是$array[ ID_OF_PRODUCT ] ,则只需执行以下操作:

unset( $_SESSION['cart'][$_POST['id']] ); // Instant access via the key!

This should be the method to use . 这应该是使用的方法 This allows you to create an array like this, with advanced info, but with easy access (42/63 are example id's) 这样,您可以创建具有高级信息但易于访问的数组(42/63是示例ID)

$_SESSION['cart']['products'][42] = array(
    'quantity' = 11,
    'size'     = 'large',
    'color'    = 'blue'
);
$_SESSION['cart']['products'][63] = array(
    'quantity' = 9,
    'size'     = 'small',
    'color'    = 'red'
);

This way you can access a lot of info with the product ID, and now also see which size and color (both just examples) the user selected. 这样,您可以使用产品ID访问大量信息,现在还可以查看用户选择的尺寸和颜色(均为示例)。 You may not have need for this now, but you will further down the road :) 您现在可能不需要此功能,但是您将继续前进:)
As you might see, you can easily do stuff with the item: 如您所见,您可以轻松地对该项目进行处理:

isset($_SESSION['cart'][$_POST['id']]); // check if the product exists
unset($_SESSION['cart'][$_POST['id']]); // remove the product
echo $_SESSION['cart'][$_POST['id']]['quantity']; // get the quantity.

Not a loop in the code. 代码中没有循环。 You should only use loops when you have to, try to somewhat avoid them because often their slow. 您仅应在必要时使用循环,请尽量避免使用它们,因为它们通常很慢。 Say you have an extreme case of 1000 items in your shop, and you need to delete no999... That'll take a noticable moment. 假设您的商店中有一种极端的情况,那就是您的商店中有1000件商品,您需要删除no999 ...这将花费大量时间。

Here is the code to do it right: 这是正确执行的代码:

$id = $_POST['id'];
$items = $_SESSION["cart"];
if(($key = array_search($id, $items)) !== false) {
    unset($items[$key]);
}
$_SESSION["cart"] = array_values($items);

Advice 忠告

Beside item ID, you can also sve item count in SESSION array because user can add several times same item into cart. 除了项目ID外,您还可以在SESSION数组中保存项目数,因为用户可以向购物车中添加多次相同的项目。 In that case your $_SESSION["card"] should be structured like: 在这种情况下,您的$ _SESSION [“ card”]的结构应为:

array(
    '1'=>12,//Item with ID=1 is added 12 times in shopping cart
    '17'=>2,//Item with ID=17 is added 2 times in shopping cart etc.
    '32'=>12,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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