【问题标题】:how to unset just one value from array session如何从数组会话中取消设置一个值
【发布时间】:2017-04-19 10:53:24
【问题描述】:
case "remove":
    if(!empty($_SESSION["cart"])) {
        foreach($_SESSION["cart"] as $k => $v) {
            if($_GET["code"] == $k){
                //print_r($_SESSION["cart"][$k]);

                unset($_SESSION["cart"][$k]);   
                header("Location:../cart.php");         
            } else {

            }
        }
    }
    break;

当我的购物车中只有一件商品时,该功能可以正常工作,但是当我有很多商品时,该商品无法删除,我该如何解决这个问题?

【问题讨论】:

    标签: php arrays session unset


    【解决方案1】:

    您需要先完成所有项目,并且仅在 foreach 结束时才能继续定位:...

    case "remove": 
        if(!empty($_SESSION["cart"])) { 
            foreach($_SESSION["cart"] as $k => $v) {
                if($_GET["code"] == $k) unset($_SESSION["cart"][$k]);   
            }
            header("Location:../cart.php");         
        }
        break;
    

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 2014-04-03
      • 2015-10-08
      • 2014-02-20
      • 1970-01-01
      • 2016-11-27
      • 2014-09-29
      • 2015-10-30
      • 2019-03-15
      相关资源
      最近更新 更多