【问题标题】:PHP 2D array fails to increment qtyPHP 2D 数组无法增加数量
【发布时间】:2014-11-25 17:53:40
【问题描述】:

我在 PHP 中更新多维数组时遇到问题。我正在尝试为一个项目实施电子商务网站,但在实施购物车时遇到问题。

基本上,我使用会话来跟踪用户添加到购物车的商品。这是我在普通伪代码中的逻辑,一旦用户在指定要为产品添加的数量后单击添加按钮,就会执行该逻辑:

从 SESSION 数组中检索“cartItems”二维数组

如果会话中不存在“cartItems”数组,则创建新的空数组并将cartItem 子数组添加到其中,其中包含数量和产品ID

ELSE 循环遍历从 SESSION 数组中检索到的数组,找到与给定产品 ID(索引 0)匹配的产品 ID,并更新该子数组(索引 1)的数量。

这是我的 PHP 脚本 addToCart.php,它反过来调用另一个脚本文件中包含的另一个函数:

<?php
require_once("cart_utility.php");
session_start();

// Script for adding a given product to the client's cart through the use of Ajax and sessions

// retrieve values from ajax request
$productID = $_GET["productID"];
$qty = $_GET["qty"];
$cartItems = null;

// use sessions to add the items to the user's cart
// retrieve the multi-dimensional cart array if it exists, otherwise create one and add it
if(isset($_SESSION["cartItems"])){
    $cartItems = $_SESSION["cartItems"];
}
else{
    $cartItems = array();
}

addQtyForProduct($productID, $qty, $cartItems);

$_SESSION["cartItems"] = $cartItems;

print "Session cartItems after function return: ";
var_dump($_SESSION["cartItems"]);

// return info string with new qty of cart items
print ("success-" . getTotalCartItems($cartItems));

?>

这是另一个处理插入和更新数组的脚本文件:

<?php

// Utility functions for retrieving items from the 2D cart items array
/* The array structure is given as (example values):
 *      | productID | qty |
 *   0  |     1     |  3  |
 *   1  |     2     |  1  |
 *   2  |     5     |  8  |
 *   3  |     8     |  3  |
 */

// increments the qty for the given product. If it does not exist then it is added into the main session array
// $cartItems: the main 2D array with the structure given above, pass by reference to change the array
function addQtyForProduct($productID, $qty, &$cartItems)
{
    foreach($cartItems as $cartItem)
    {
        var_dump($cartItem);
        if($cartItem[0] == $productID){
            //print "Quantity given to increment: $qty";
            //var_dump($cartItem);
            print "Qty in current session array: $cartItem[1]";
            $cartItem[1] += $qty;
            print "New qty in cartItem array: $cartItem[1]";
            return;
        }
    }

    // not found, therefore add it to the main items array
    array_push($cartItems, array($productID, $qty));
}

// returns the total number of items in the cart
function getTotalCartItems($cartItems)
{
    $total = 0;

    foreach($cartItems as $cartItem)
        $total += $cartItem[1];

    return $total;
}

?>

我已经放置了一些 var_dump 语句,并且可以确认在从函数“addQtyForProduct”返回时,数组没有更新。但为什么?我通过引用传递数组来直接改变它的内容。

第一次不存在数组时添加成功,如果数组存在则增加失败。

此外,值在“addQtyForProduct”函数中成功递增,但数组从函数返回时未更新。

我很乐意为此提供一些帮助。这几天我一直在努力理解这一点,这让我发疯了。

【问题讨论】:

  • 为什么不将productID存储为key,数量存储为value?这样你会得到以下信息:array([10] =&gt; 8) 这意味着你有 8 数量的产品 10
  • 感谢您的建议。我觉得有一种更好的方法,但我已经让它工作了,只需要一个&符号......我想如果我通过引用传递主数组,那么我将拥有所有存储项目的地址里面也...

标签: php arrays session multidimensional-array


【解决方案1】:

this page 上阅读时,您应该使用引用。在您的$cartItem 前面添加一个&amp;,您的脚本应该可以工作。现在 PHP 将数组中每个值的“副本”存储在 $cartItem 中,而不是它的引用。因此,目前您正在编辑原始数组的副本,而不是原始数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多