Delete Array Index

So I’m in the middle of a predicament here. Currently what I’m attempting to do is to delete the index of an array. But at the same time, I’m using the current() and next() functions. So what I have now is this:
[php]$i = 4; // Index of array to delete
unset($array[$i]);
$array = array_unique($array);
while(key(current($array)) != $i) {
next($array);
}[/php]
I’m also aware of the array_splice function, but as far as I know that also destroys the current() data. I’m also a bit confused on how I would use the array_splice() function… from what I gather I, to remove just a single index and keep everything around it, it would be something along the lines of…?
[php]$i = 4; // Index of array to delete
array_splice($array, $i, 1);[/php]
Correct me if I’m wrong on that, and I too believe that the current() value of the array is then reset too. Is there a workaround for this? All I want to do is remove a single index from an array.

Also, is it possible to add an index to an array between two others? For example, I have a list of {“apples”, “grapes”, “oranges”}, but I want to add “cherries” to index 1, so the array is {“apples”, “cherries”, “grapes”, “oranges”}. Is that possible? Thanks!

Can you not use unset()?

[php]unset($array[‘index’]);[/php]

Yeah, but that leaves empty indexes. Like if I do next() and it’s an empty index, it’ll be empty and I can’t have that xP

If you use:

[php]foreach($array as $key => $element)
{
//do stuff
}[/php]

You should be able to delete elements to your hearts content.

Sponsor our Newsletter | Privacy Policy | Terms of Service