Adeus Posted March 25, 2008 Share Posted March 25, 2008 I'm trying to delete an element from my array. I have two dimensions; $_SESSION['cart'] = array( array("10", "something1"), array("20", "something2") ); I am trying to remove one of the element arrays which are inside the session array, in example $_SESSION['cart']['1'] (which would be 20, something2). When I try the following code, it does not remove the array element; if (isset($_POST['remove'])) { $remove = $_POST['remove']; array_splice($_SESSION['cart'], $remove, 1); } Shouldn't this remove the second element in my $_SESSION['cart'] array? I use POST because it is a shopping cart, and to remove an item I have link to the same page with a post (remove=1). Do I need to use unset since the parent array is associative? Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/ Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 what value will $_POST['remove'] have in this example? Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-500757 Share on other sites More sharing options...
Adeus Posted March 25, 2008 Author Share Posted March 25, 2008 The $_POST['remove'] value is 1, but will be any number depending on which link is followed. It is sent from a link on the same page (samepage.php?remove=1). I tested the POST with an echo just to make sure, and it came through as 1. Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-500829 Share on other sites More sharing options...
teng84 Posted March 25, 2008 Share Posted March 25, 2008 http://www.phpfreaks.com/forums/index.php/topic,189198.0.html Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-500832 Share on other sites More sharing options...
Adeus Posted March 27, 2008 Author Share Posted March 27, 2008 Teng, Thanks for the reply but I still have not solved my problem. The example in Reply #2 does not seem to apply, since I am using a key to determine which element to delete. I tried using Barand's suggestion, still to no avail; if (isset($_POST['remove'])) { $remove = $_POST['remove']; unset ($_SESSION['cart'][$remove]); } Any ideas? Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-502452 Share on other sites More sharing options...
discomatt Posted March 27, 2008 Share Posted March 27, 2008 If the link is in the url it's using the GET method rather than POST. Try $_GET['remove'] Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-502459 Share on other sites More sharing options...
redarrow Posted March 27, 2008 Share Posted March 27, 2008 <?php session_start(); $_SESSION['cart'] = array("10", "something1","20", "something2"); unset($_SESSION['cart'][0],$_SESSION['cart'][1]); foreach($_SESSION['cart'] as $_SESSION['cart']){ echo " ".$_SESSION['cart']."<br> "; } ?> Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-502472 Share on other sites More sharing options...
Adeus Posted March 27, 2008 Author Share Posted March 27, 2008 If the link is in the url it's using the GET method rather than POST. Try $_GET['remove'] D'oh!! Silly me... thanks so much. Here is the final working example. Thanks everyone. if (isset($_GET['remove'])) { //check if item is being removed $remove = $_GET['remove']; //set variable to reflect which element to remove depending on key unset ($_SESSION['cart'][$remove]); //remove element $_SESSION['cart'] = array_values($_SESSION['cart']); //sort the array to eliminate gaps in keys } Link to comment https://forums.phpfreaks.com/topic/97864-delete-item-from-array/#findComment-502515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.