我正在做一个购物车项目,我在会话中添加了一些项目作为数组,但当我添加一个项目时,它会显示两个项目,代码如下:
if(!empty($_GET['pid'])) {
if(!empty($_SESSION['cart'])) {
$item = array($_GET['id'], $_GET['pid'], $_GET['item_weight'], $_GET['item_quantity'], $_GET['per_item_price'], $_GET['total_price'], $_GET['savings'], $_GET['product_name'], $_GET['type']);
$index = count($_SESSION['cart']);
$_SESSION['cart'][$index] = $item;
} else {
$item = array($_GET['id'], $_GET['pid'], $_GET['item_weight'], $_GET['item_quantity'], $_GET['per_item_price'], $_GET['total_price'], $_GET['savings'], $_GET['product_name'], $_GET['type']);
$_SESSION['cart'] = array($item);
}
}你知道我的代码出了什么问题吗?
发布于 2014-02-13 17:32:24
您可以更简单地使用[]将项推送到数组中:
$_SESSION['cart'][] = array(
'some' => 'stuff'
);发布于 2014-02-13 17:30:34
但是你的问题是你已经用数组2次了..因为$item已经是数组的形式了..再次尝试将数组存储到$_SESSION['cart']
$item = array($_GET['id'], $_GET['pid'], $_GET['item_weight'], $_GET['item_quantity'], $_GET['per_item_price'], $_GET['total_price'], $_GET['savings'], $_GET['product_name'], $_GET['type']);
//$_SESSION['cart'] = array($item);
$_SESSION['cart'] = $item; https://stackoverflow.com/questions/21749984
复制相似问题