Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将值推送到数组PHP的正确方法?_Php_Arrays_Json - Fatal编程技术网

将值推送到数组PHP的正确方法?

将值推送到数组PHP的正确方法?,php,arrays,json,Php,Arrays,Json,我遇到了一个有点恼人的问题。这是我的PHP代码。忽略变量的来源。这是购物车功能,但它适用于许多不同的领域 $data_set = json_decode(stripslashes($_POST['varA']), true); $pid = $pid['product_id']; $quantity = $pid['quantity']; $_SESSION['cartid'] = $_SESSION['cartid'] + 1; $product_data = array("Product

我遇到了一个有点恼人的问题。这是我的PHP代码。忽略变量的来源。这是购物车功能,但它适用于许多不同的领域

$data_set = json_decode(stripslashes($_POST['varA']), true);
$pid = $pid['product_id'];
$quantity = $pid['quantity'];

$_SESSION['cartid'] = $_SESSION['cartid'] + 1;

$product_data = array("Product_ID" = > $pid, "quantity" = > $quantity, "cartid" = > $_SESSION['cartid']);
我的问题发生在代码中的这个位置。我首先检查会话变量中是否有值,如果没有,则继续创建关联数组

if (empty($_SESSION['cart_items'])) {
    $_SESSION['cart_items'] = array("items" = > $product_data);
} else {
    array_push($_SESSION['cart_items']['items'], $product_data);
}


echo json_encode($_SESSION['cart_items']);
“添加”第一项后的最终结果如下所示:

{
    "items": {
        "Product_ID": "2",
        "quantity": "1",
        "cartid": 1
    }
}
但是,在第一次添加几次后,每个值都会得到一个键:

{
    "items": {
        "0": {
            "Product_ID": "2",
            "quantity": "1",
            "cartid": 2
        },
        "1": {
            "Product_ID": "2",
            "quantity": "1",
            "cartid": 3
        },
        "Product_ID": "2",
        "quantity": "1",
        "cartid": 1
    }
}
如何防止这些密钥发生?这可能吗?如果没有,如何重新写入,以便每次都添加密钥?这是否可以在前端的JS中进行解析和循环


对不起,我有这么多问题。非常感谢您的帮助。

这是因为这句话:

$_SESSION['cart_items'] = array("items" = > $product_data);
实际上,您在该行中说“items”键具有产品数据,而不是项中的键。应该是:

$_SESSION['cart_items']['items'] = array($product_data);
键-将-始终出现,除非您希望数据覆盖另一个。如果不需要键(0、1等),则唯一的其他选项是合并数据。在这种情况下:

$\会话['cart\u items']['items']+=$product\u数据


…但我认为这不是您想要的。

您不需要
项,请尝试以下方法

if (empty($_SESSION['cart_items'])) {
    $_SESSION['cart_items'] = array($product_data);
} else {
    $_SESSION['cart_items'][] = $product_data;
}

在第一次迭代中,
$\u会话['cart\u items']
是空的,因此运行以下操作:

$_SESSION['cart_items'] = array("items" => $product_data);
这将创建
$\u会话['cart\u items']['items']
,但您只使用产品本身填充它;您应该将其定义为数组:

$_SESSION['cart_items'] = array("items" => array($product_data));
这将创建一个包含单个项的数组,稍后可以使用
array\u push
进行扩展

话虽如此,您可以将整个条件替换为:

$_SESSION['cart_items']['items'][] = $product_date;
如果不存在空数组,PHP将自动创建一个空数组,然后添加产品数据作为下一个元素。

您希望它看起来像什么?