rondog Posted July 12, 2008 Share Posted July 12, 2008 I have a $_SESSION thats an array of some values..I need to be able to remove certain items in them and reset their index's. Ive tried unset, but it removed everything in the array. Is their an easy way to do this?? Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/ Share on other sites More sharing options...
rondog Posted July 12, 2008 Author Share Posted July 12, 2008 ok I kind of got it working with unset.. <?php if(isset($_GET['remove'])) { unset($_SESSION['recipients'][$_GET['remove']]); } ?> It works, but if their is only one left, it doesnt remove it for some reason :[ Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-588217 Share on other sites More sharing options...
Ken2k7 Posted July 12, 2008 Share Posted July 12, 2008 Perhaps you can read up array_splice(). php.net's array_splice() Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-588219 Share on other sites More sharing options...
Barand Posted July 12, 2008 Share Posted July 12, 2008 It works, but if their is only one left, it doesnt remove it for some reason :[ weird, this snippet works fine <?php $a = array(1,2,3); echo '<pre>', print_r($a, true), '</pre>'; for ($i=0; $i< 3; $i++) { unset($a[$i]); echo '<pre>', print_r($a, true), '</pre>'; } ?> --> Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [1] => 2 [2] => 3 ) Array ( [2] => 3 ) Array ( ) Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-588288 Share on other sites More sharing options...
rondog Posted July 14, 2008 Author Share Posted July 14, 2008 Yeah my code is practically doing the same thing right barand?..the unset part anyway.. I've noticed if you go from the last item in the array and work your way back, so if their are 3 items and you remove position 2, then 1, then 0, it works fine, but if you start in the middle or the beginning it acts weird. Check it out here => http://ronnieswietek.com/flashden/newsletter/newsletter.php?do=preview&id=1 (you will need to do it twice because you need to login) u: admin p: testadmin select all for the recipients and then hit review newsletter and then test out the remove function Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-589999 Share on other sites More sharing options...
Barand Posted July 14, 2008 Share Posted July 14, 2008 Mine starts at the beginning and works fine. I also tried working backwards from the end and also no problems ??? Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590012 Share on other sites More sharing options...
rondog Posted July 14, 2008 Author Share Posted July 14, 2008 Not sure whats going on here then. Maybe Ill try and look into array_splice because using unset seems it should be easier than this and according to your example it is, however mine is acting up and the code I posted in post#2 is exactly what I have to remove the user from the list. Did you see how it acted in the link I provided? Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590022 Share on other sites More sharing options...
rondog Posted July 15, 2008 Author Share Posted July 15, 2008 I still cant get this working...so my remove links are ?remove=0, ?remove=1, ?remove=2, ?remove=3 etc..... my code to remove is: <?php if(isset($_GET['remove'])) { $loc = $_GET['remove']; unset($_SESSION['recipients'][$loc]); } ?> printing my array is: Array ( [0] => 1 [1] => 2 [2] => 3 ) the 1, 2, 3 are the IDs in my database so I can use those to get certain data. Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590758 Share on other sites More sharing options...
rondog Posted July 15, 2008 Author Share Posted July 15, 2008 I think what I need to do is reset the array index...If I remove array[0], it outputs array[1],array[2]. Once its removed I need it to be array[0],array[1] Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590763 Share on other sites More sharing options...
kenrbnsn Posted July 15, 2008 Share Posted July 15, 2008 I wrote a quick test script: <?php session_start(); $rec = range(1,10); if(isset($_GET['remove'])) { $loc = $_GET['remove']; echo '<pre>Before: ' . print_r($_SESSION['recipients'],true) . '</pre>'; unset($_SESSION['recipients'][$loc]); echo '<pre>After : ' . print_r($_SESSION['recipients'],true) . '</pre>'; } else $_SESSION['recipients'] = $rec; ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <?php foreach ($_SESSION['recipients'] as $i => $v) echo '<a href="?remove=' . $i . '">Remove #' . $i . '</a><br>'; ?> </body> </html> And it works fine. Compare my code to yours. Ken Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590773 Share on other sites More sharing options...
rondog Posted July 15, 2008 Author Share Posted July 15, 2008 Ahh I think my issue was not with my remove function, but rather my function of displaying the data aftewards. I was using a for loop: for($i = 0; $i < count($_SESSION['recipients']); $i++) And I used your foreach loop and so far I am not having any issues. I think for now this topic is solved thanks! Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590781 Share on other sites More sharing options...
947740 Posted July 15, 2008 Share Posted July 15, 2008 Then click on the topic solved link Link to comment https://forums.phpfreaks.com/topic/114386-solved-remove-item-in-array-by-index/#findComment-590785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.