PHP 数组问题

发布于 2024-11-27 02:58:51 字数 301 浏览 0 评论 0原文

我正在为朋友构建一个简单的购物车,并在会话中使用数组来存储它。

要将商品添加到购物车,我有以下代码

$next_item = sizeof($_SESSION['cart']) +1;
$_SESSION['cart'][$next_item] = array(item => $product_id, option => $option, qty => 1);

:如果有人添加另一个相同的商品或更新购物车,我正在努力解决如何更新此数组中的商品数量的问题。有人能指出我正确的方向吗?谢谢

I am building a simple cart for a friend and using an array in session to store it.

To add an item to the cart I have this code

$next_item = sizeof($_SESSION['cart']) +1;
$_SESSION['cart'][$next_item] = array(item => $product_id, option => $option, qty => 1);

What I'm struggling with how to update an item's quantity in this array should someone add another of the same item or update the cart. Can anyone point me in the right direction? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

本宫微胖 2024-12-04 02:58:51

就像

foreach($_SESSION['cart'] as $key => $value) {

    if ($_SESSION['cart'][$key]['item'] == $product_id) {

        $_SESSION['cart'][$key]['qty'] += $qty_to_add;
    }
}

我会改变你的数组的结构。

而不是

$_SESSION['cart'] = array(
    1 => array(
        'item' => 1,
        'option' => 1,
        'qty' => 1),
    2 => array(
        'item' => 2,
        'option' => 1,
        'qty' => 1),
    3 => array(
        'item' => 3,
        'option' => 1,
        'qty' => 1)
);

Use

$_SESSION['cart'] = array(
    1 => array(
        'option' => 1,
        'qty' => 1),
    2 => array(
        'option' => 1,
        'qty' => 1),
    3 => array(
        'option' => 1,
        'qty' => 1)
);

其中键是产品 id。这将使参考项目更容易,您可以在一行中更新数量

$_SESSION['cart'][$product_id]['qty'] += $qty_to_add;

Something like

foreach($_SESSION['cart'] as $key => $value) {

    if ($_SESSION['cart'][$key]['item'] == $product_id) {

        $_SESSION['cart'][$key]['qty'] += $qty_to_add;
    }
}

I would change the structure of your array.

Instead of

$_SESSION['cart'] = array(
    1 => array(
        'item' => 1,
        'option' => 1,
        'qty' => 1),
    2 => array(
        'item' => 2,
        'option' => 1,
        'qty' => 1),
    3 => array(
        'item' => 3,
        'option' => 1,
        'qty' => 1)
);

Use

$_SESSION['cart'] = array(
    1 => array(
        'option' => 1,
        'qty' => 1),
    2 => array(
        'option' => 1,
        'qty' => 1),
    3 => array(
        'option' => 1,
        'qty' => 1)
);

Where the key is the product id. It will make referencing the items easier and you can update the quantity in one line

$_SESSION['cart'][$product_id]['qty'] += $qty_to_add;
不奢求什么 2024-12-04 02:58:51

如果顺序不重要,您可以将产品存储在关联数组中。

if (isset($_SESSION['cart'][$product_id])) {
    // set qty of $_SESSION['cart'][$product_id] + 1
} else {
    // create $_SESSION['cart'][$product_id] with qty of 1
}

If order is not important you can store your products in an associative array.

if (isset($_SESSION['cart'][$product_id])) {
    // set qty of $_SESSION['cart'][$product_id] + 1
} else {
    // create $_SESSION['cart'][$product_id] with qty of 1
}
冷情妓 2024-12-04 02:58:51

首先,您不需要计算数组大小:

$_SESSION['cart'][] = array(...);

其次,我将使用 $product_id 作为数组键。这样,搜索就很简单:

if( isset($_SESSION['cart'][$product_id]) ){
    $_SESSION['cart'][$product_id]['qty']++;
}else{
    $_SESSION['cart'][$product_id] = array(
        'option' => $option,
        'qty' => 1,
    );
}

First, you don't need to calculate the array size:

$_SESSION['cart'][] = array(...);

Second, I would use $product_id as array key. That ways, searches are straightforward:

if( isset($_SESSION['cart'][$product_id]) ){
    $_SESSION['cart'][$product_id]['qty']++;
}else{
    $_SESSION['cart'][$product_id] = array(
        'option' => $option,
        'qty' => 1,
    );
}
林空鹿饮溪 2024-12-04 02:58:51

我不能说你为它选择了一个好的结构。如何在 $product_id 上建立索引呢?这样,您将始终知道您的购物车中是否已有特定商品:

<?php
     if( isset($_SESSION['cart'][$product_id]) ) {
        $_SESSION['cart'][$product_id]['qty'] += $new_qty;
     } else {
        $_SESSION['cart'][$product_id] = array(item => $product_id, option => $option, qty => 1);
     }
 ?>

I can't say that you've chosen a good structure for it. How about indexing upon the $product_id instead? That way, you'll always know if you already have a specific item in your cart:

<?php
     if( isset($_SESSION['cart'][$product_id]) ) {
        $_SESSION['cart'][$product_id]['qty'] += $new_qty;
     } else {
        $_SESSION['cart'][$product_id] = array(item => $product_id, option => $option, qty => 1);
     }
 ?>
落墨 2024-12-04 02:58:51

要将商品添加到购物车,只需使用此命令(假设产品 ID 是唯一的):

$_SESSION['cart'][$product_id] = array('item' => $product_id, 'option' => $option, 'qty' => 1);

要将任何给定产品 ID 的数量设置为 5,请使用此命令:

$_SESSION['cart'][$product_id]['qty'] = 5;

要将产品数量增加 3,请使用此命令:

$_SESSION['cart'][$product_id]['qty'] += 3;

To add something to the cart, simply use this (assuming product ids are unique):

$_SESSION['cart'][$product_id] = array('item' => $product_id, 'option' => $option, 'qty' => 1);

To set the quantity for any given product id to 5, use this:

$_SESSION['cart'][$product_id]['qty'] = 5;

To increase the qty of a product by 3, use this:

$_SESSION['cart'][$product_id]['qty'] += 3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文