I'm having a little trouble with losing my array order after using unset(). Here's the code first:
$id = $_GET['id'];
for($i = 0; $i < count($my_array); $i++)
{
if($my_array[$i] == $id)
{
unset($my_array[$i]);
}
}
Assume that $my_array has 4 items and $my_array[1] is equal to $id. After I unset that I try to run a foreach() on $my_array and I get and Undefined Offset: 1 error. When I run print_r($my_array), I get $my_array[0], $my_array[2], and $my_array[3].
I understand perfectly why that's happening, my question is, is there a way to re-index the array so that item 2 'drops' to item 1, and and the rest of the items respectively to the end of the array?
Something like reindex($my_array) would be sweet. I know I could run another for loop with a new array and transfer them manually, but a one step solution would be awesome. I just couldn't find anything anywhere.