Soh*_*lik 2 php session multidimensional-array
我在使用$_SESSIONPHP变量更新数组元素时遇到问题.这是基本结构:
$product = array();
$product['id'] = $id;
$product['type'] = $type;
$product['quantity'] = $quantity;
Run Code Online (Sandbox Code Playgroud)
然后通过使用array_push()函数我在SESSION变量中插入该产品.
array_push($_SESSION['cart'], $product);
Run Code Online (Sandbox Code Playgroud)
现在这是我面临问题的主要部分:
foreach($_SESSION['cart'] as $product){
if($id == $product['id']){
$quantity = $product['quantity'];
$quantity += 1;
$product['quantity'] = $quantity;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在$_SESSION['cart']变量中增加产品数量.我怎样才能做到这一点?
Mar*_*c B 13
不要盲目地将产品塞进会话中.使用产品的ID作为密钥,然后在购物车中查找/操作该项目是微不足道的:
$_SESSION['cart'] = array();
$_SESSION['cart'][$id] = array('type' => 'foo', 'quantity' => 42);
$_SESSION['cart'][$id]['quantity']++; // another of this item to the cart
unset($_SESSION['cart'][$id]); //remove the item from the cart
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24713 次 |
| 最近记录: |