Jump to content

[SOLVED] Remove item in array by index


rondog

Recommended Posts

 

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
(
)

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

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?

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.

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

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 :D

 

thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.