Jump to content

Unsetting a specific element of an Array


Dubya008

Recommended Posts

I am using session variables to keep track of what is in a shopping cart. I can add the items to the cart quite easily enough. But my problem is that I can't delete a specific item in the cart. Below is a summary of what the array looks like

 

$Item1=array("ProdID"=>"Hat","Color"=>"Not Needed","Size"=>5);

$Item4=array("ProdID"=>"Shirt","Color"=>"Green","Size"=>"L");

$Item2=array("ProdID"=>"Pants","Color"=>"Not Needed","Size"=>"32x32");

$Item3=array("ProdID"=>"Socks","Color"=>"Not Needed","Size"=>"Not Needed");

$NumItems=4;

$TestArray=array("NumItems"=>$NumItems,1=>$Item1,2=>$Item2,3=>$Item3,4=>$Item4);

 

However when I attempt to delete any of the top level elements (1,2,3,4,NumItems), I either end up deleting only the last element (4) or somehow taking out 2 of them. I know that this must be an easy fix but I've looked at it for so long that I can't seem to figure out what the problem is.

below is the code that I use to delete the Item from the array

$ItemNum=$_GET['item'];
		if ($ItemNum=="All"){
			$ItemNum=$_GET['item'];
		if ($ItemNum=="All"){
			unset($TestArray);
			unset($TestArray['NumItems']);
			unset($_SESSION['OrderTotal']);
			//header("Location:Cart.php?act=view");
		}
		else{
			$TestArray['NumItems']=$TestArray['NumItems']-1;
			if($TestArray['NumItems']<1)
			{
				unset($_SESSION['OrderTotal']);
				unset($TestArray);
				unset($TestArray['NumItems']);
			}
			else
			{
				unset($TestArray[$ItemNum]);
			}

 

Can anyone take a look at this and point me in the correct direction? What I am after is to delete any element of the top array and keep the rest.

 

Link to comment
Share on other sites

if ($ItemNum=="All"){

  $ItemNum=$_GET['item'];

if ($ItemNum=="All"){

  unset($TestArray);

 

Why do you have the same validation check here??

 

unset($TestArray);

unset($TestArray['NumItems']);

 

Why do you first delete the whole variable then one of its elements? Doesn't make sense.

 

$TestArray['NumItems']=$TestArray['NumItems']-1;

 

can be rewritten to:

 

$TestArray['NumItems']--

 

if($TestArray['NumItems']<1) I assume it can't be negative? So it can only be zero.

 

Then write:

 

if(!$TestArray['NumItems'])

 

Link to comment
Share on other sites

okay So i made the changes that you suggested. It is now deleting 2 of the elements at once. Would I need to rebuild the entire array after I've deleted the element of the array?

 

$Item1=array("ProdID"=>"Hat","Color"=>"Not Needed","Size"=>5);
$Item4=array("ProdID"=>"Shirt","Color"=>"Green","Size"=>"L");
$Item2=array("ProdID"=>"Pants","Color"=>"Not Needed","Size"=>"32x32");
$Item3=array("ProdID"=>"Socks","Color"=>"Not Needed","Size"=>"Not Needed");
$NumItems=4;
$_SESSION['Cart']=array(1=>$Item1,2=>$Item2,3=>$Item3,4=>$Item4,"NumItems"=>$NumItems);

$ItemNum=$_GET['id'];
		if ($ItemNum=="All"){								//deleting everything in the  cart
			unset($_SESSION['Cart']);
		}
		else{
			$_SESSION['Cart']['NumItems']--;	//taking the number of items down by 1
			if(!$_SESSION['Cart']['NumItems'])	//checking to see if the number of items is above zero
			{
				unset($_SESSION['Cart']);			//if the number is above zero then delete the whole variable
			}
			else													//deleting a specific element of the array
			{
				echo "DELETING ".$_GET['id']."<br>";
				unset($_SESSION['Cart'][$ItemNum]);
			}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.