Dubya008 Posted January 11, 2012 Share Posted January 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254827-unsetting-a-specific-element-of-an-array/ Share on other sites More sharing options...
silkfire Posted January 11, 2012 Share Posted January 11, 2012 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']) Quote Link to comment https://forums.phpfreaks.com/topic/254827-unsetting-a-specific-element-of-an-array/#findComment-1306660 Share on other sites More sharing options...
Dubya008 Posted January 12, 2012 Author Share Posted January 12, 2012 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]); } Quote Link to comment https://forums.phpfreaks.com/topic/254827-unsetting-a-specific-element-of-an-array/#findComment-1306768 Share on other sites More sharing options...
silkfire Posted January 12, 2012 Share Posted January 12, 2012 Invert your logic by removing the exclamation mark. ! means NOT. Quote Link to comment https://forums.phpfreaks.com/topic/254827-unsetting-a-specific-element-of-an-array/#findComment-1306808 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.