halim Lim
halim Lim

Reputation: 1

how to unset just one value from array session

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;

when I just have one item in my cart, the function work well, but when I have many item, the item cannot delete, how can I solve this problem?

Upvotes: 0

Views: 93

Answers (1)

Shai Shprung
Shai Shprung

Reputation: 146

You need to finish all the items first and only at the end of the foreach to continue with the location:...

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;

Upvotes: 1

Related Questions