Php 购物车数组检查项目是否已添加
Php cart array check if item is already added
我正在尝试开发一个带有购物车的电子商务应用程序,主要使用 php 和 jquery。
我需要将所选项目(id、sku、尺寸和颜色)与数组中已有的项目进行比较。如果该项目已存在于数组中,则将数量增加所选项目的数量。
含义:
- 客户点击了一个项目"add to cart"
- 客户再次点击该项目
- PHP函数检查数组中是否存在新增项,存在则增加数量,不存在则压入数组
你可以这样写代码。
$cart = $_SESSION['cart'];
/*
* If product already exist into the cart then update QTY of product
* Othewise add new product into the cart
*/
if(isset($cart[$product['id']])):
$cart[$product['id']]['qty'] += 1;
else:
$cart[$product['id']] = $product;
$cart[$product['id']]['qty'] = 1;
endif;
$_SESSION['cart'] = $cart;
我正在尝试开发一个带有购物车的电子商务应用程序,主要使用 php 和 jquery。
我需要将所选项目(id、sku、尺寸和颜色)与数组中已有的项目进行比较。如果该项目已存在于数组中,则将数量增加所选项目的数量。
含义:
- 客户点击了一个项目"add to cart"
- 客户再次点击该项目
- PHP函数检查数组中是否存在新增项,存在则增加数量,不存在则压入数组
你可以这样写代码。
$cart = $_SESSION['cart'];
/*
* If product already exist into the cart then update QTY of product
* Othewise add new product into the cart
*/
if(isset($cart[$product['id']])):
$cart[$product['id']]['qty'] += 1;
else:
$cart[$product['id']] = $product;
$cart[$product['id']]['qty'] = 1;
endif;
$_SESSION['cart'] = $cart;