Solarpitch Posted December 6, 2008 Share Posted December 6, 2008 Hey, I have the floowing code to add an id to an array but how can I remove an id from the array if I needed? <?php $id = $_GET['id']; if(!is_array($_SESSION['compare'])) { $_SESSION['compare'] = array(); } if(!in_array($id, $_SESSION['compare'])) { $_SESSION['compare'][] = $id; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135818-solved-remove-an-item-from-an-array/ Share on other sites More sharing options...
waynew Posted December 6, 2008 Share Posted December 6, 2008 Have a read: http://ie2.php.net/array Quote Link to comment https://forums.phpfreaks.com/topic/135818-solved-remove-an-item-from-an-array/#findComment-707882 Share on other sites More sharing options...
Solarpitch Posted December 6, 2008 Author Share Posted December 6, 2008 Thanks but I already did. See, I'm using unset in the below context but instead of removing the id, it removes all of the id's which I cant find the solution to on that page <?php unset($id, $_SESSION['compare']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/135818-solved-remove-an-item-from-an-array/#findComment-707887 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 unset($_SESSION['compare'][array_search($id, $_SESSION['compare'])]); Should get you the desired result. Quote Link to comment https://forums.phpfreaks.com/topic/135818-solved-remove-an-item-from-an-array/#findComment-707892 Share on other sites More sharing options...
Solarpitch Posted December 6, 2008 Author Share Posted December 6, 2008 That worked. Thanks... id never of figured that out. Quote Link to comment https://forums.phpfreaks.com/topic/135818-solved-remove-an-item-from-an-array/#findComment-707896 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.