我正在尝试创建添加到购物车功能。我正在使用PHP array_merge
函数将值合并到购物车。但是行在数组中是重复的。我需要帮助来解决这个问题。
if(isset($_SESSION["cart_item"])) {
if(is_array($_SESSION["cart_item"])){
if(array_key_exists($productByCode[0]["code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $_SESSION["cart_item"][$k]['code']){
$_SESSION["cart_item"][$k]["quantity"] = $_SESSION["cart_item"][$k]["quantity"] + $_POST["quantity"];
break;
}
}
} elseif(!array_key_exists($productByCode[0]["code"],$_SESSION["cart_item"]) && !in_array($productByCode[0]["name"], $_SESSION["cart_item"])) {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
}
}elseif(!isset($_SESSION["cart_item"])){
$_SESSION["cart_item"] = $itemArray;
}