MDanz Posted October 28, 2010 Share Posted October 28, 2010 $itemlisted = $_POST['itemlisted']; if ($itemlisted) { unset($_SESSION['items'][$itemlisted]); } it should work i don't get it. $itemlisted is 1... so it should delete [1] in the array. any idea why it's not working? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 28, 2010 Share Posted October 28, 2010 Are you even sure that the IF statement is resulting to TRUE? Your code is assuming values are present and you shouldn't use a variable as the condition of a comparison to check if the value is set or has a value. What does the following output? $itemlisted = (isset($_POST['itemlisted'])) ? trim($_POST['itemlisted']) : ''; if (!empty($itemlisted)) { echo "Posted value: {$itemlisted}<br />"; echo "SESSION Before unset: <br />"; print_r($_SESSION); unset($_SESSION['items'][$itemlisted]); echo "SESSION After unset: <br />"; print_r($_SESSION); } Quote Link to comment Share on other sites More sharing options...
MDanz Posted October 28, 2010 Author Share Posted October 28, 2010 it prints this Posted value: 1 SESSION Before unset: Array ( [url] => http://www.eg.com/cart.php [items] => Array ( [4] => 13 [5] => 13 [6] => 13 [7] => 14 ) ) SESSION After unset: Array ( [url] => http://www.eg.com/cart.php [items] => Array ( [4] => 13 [5] => 13 [6] => 13 i think i know... why is the Array not starting from [0] ? Quote Link to comment Share on other sites More sharing options...
MDanz Posted October 28, 2010 Author Share Posted October 28, 2010 does anyone know why the array is not starting from zero. i get this Posted value: 1 SESSION Before unset: Array ( [4] => 13 [5] => 13 [6] => 13 [7] => 14 ) SESSION After unset: Array ( [4] => 13 [5] => 13 [6] => 13 [7] => 14 ) when i use this code $itemlisted = (isset($_POST['itemlisted'])) ? trim($_POST['itemlisted']) : ''; if (!empty($itemlisted)) { echo "Posted value: {$itemlisted}<br />"; echo "SESSION Before unset: <br />"; print_r($itemid); unset($_SESSION['items'][$itemlisted]); echo "SESSION After unset: <br />"; print_r($itemid); } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 We'd need to see how the array is being constructed. Quote Link to comment Share on other sites More sharing options...
MDanz Posted October 28, 2010 Author Share Posted October 28, 2010 if(isset($_GET['id'])) { $_SESSION['items'][]=$id; } $itemid = $_SESSION['items']; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 On each subsequent submission, the index is added to the end of the array, so if the array isn't re-initialized, they will just keep incrementing. Quote Link to comment Share on other sites More sharing options...
MDanz Posted October 28, 2010 Author Share Posted October 28, 2010 how do i re-initialize the array? array_values i've tried array_values($itemid); that makes it start from zero... but i don't know how to set it as a session? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 $_SESSION['items'] = array(); will re-initialize it as an empty array. Without knowing more about your application, I don't know if it's appropriate to do so or not, though. You'll have to decide that. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 28, 2010 Share Posted October 28, 2010 Echoin'g Pickachu's comment, it would be better to know what exactly you are doing, but this might work as a "quick fix" to reindex the values starting at 0. $_SESSION['items'] = array_values($_SESSION['items']); Quote Link to comment 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.