RE: [PHP] Can't remove an element from an array

From: Date: Thu, 19 Feb 2004 17:14:47 +0000
Subject: RE: [PHP] Can't remove an element from an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-178263@lists.php.net to get a copy of this message
You have two separate problems here: 1) unset($_SESSION["cart_array"], $item) This unsets $_SESSION["cart_array"] and also $item, it should be unset($_SESSION["cart_array"][$item]) 2) The method of unsetting session variables depends on whether you have register_globals on, it's all here: http://www.php.net/manual/en/ref.session.php . The Examples section has detailed examples that should help you fix your problem > -----Original Message----- > From: Brian Dunning [mailto:brian@briandunning.com] > Sent: Thursday, February 19, 2004 8:50 AM > To: php-general@lists.php.net > Subject: [PHP] Can't remove an element from an array > > > 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#178263) next »