Re: Can't remove an element from an array

From: Date: Thu, 19 Feb 2004 16:58:29 +0000
Subject: Re: Can't remove an element from an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-178262@lists.php.net to get a copy of this message
Brian Dunning wrote:
Hi, I have an array that holds the item_id's for a shopping cart. I am trying to remove the item specified by a GET like this: foreach ($_SESSION["cart_array"] as $item) { if ($item==$_GET["item_id"]) {unset($_SESSION["cart_array"], $item); break;} } But it removes ALL items from the cart. And then when I view the cart on the next page, EVERYTHING is back, including the item I tried to remove. What am I doing wrong? Thanks! - Brian
Because you're unsetting the WHOLE 'cart_array'. If you're 'cart_array' is set up like this.... Cart -> Item0 -> stuff stuff Item1 -> stuff stuff Item2 -> stuff stuff And someone wants to delete Item1 from their cart.... unset ( $_SESSION['cart_array']['Item1'] ); http://us4.php.net/manual/en/function.unset.php -- By-Tor.com It's all about the Rush http://www.by-tor.com

« previous php.general (#178262) next »