为什么在具有数组合并功能的PHP数组中重复行?

时间:2017-04-05 22:29:47

标签: php arrays function

我正在尝试创建添加到购物车功能。我正在使用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;
}

0 个答案:

没有答案