Hi,
I was trying to filter the contents in an array, and it seems to work, but I've got an issue with the indexes. When I unset() an element, if I count() the number of elements, I still get the same number that I had before filtering. I'd also like to make my indexes go back to 0,1,2,3...
For example, I want to filter the elements containing the string "apple". If I had this array:
$fruits [0] = "apple";
$fruits [1] = "lemon";
$fruits [2] = "orange";
$fruits [3] = "lemon";
$fruits [4] = "strawberry";
$fruits [5] = "apple";
$fruits [6] = "lemon";
I'd like to make it:
$fruits [0] = "lemon";
$fruits [1] = "orange";
$fruits [2] = "lemon";
$fruits [3] = "strawberry";
$fruits [4] = "lemon";
count($fruits) should return 5.
But I'm getting:
$fruits [1] = "lemon";
$fruits [2] = "orange";
$fruits [3] = "lemon";
$fruits [4] = "strawberry";
$fruits [6] = "lemon";
count($fruits) returns 7.
Here is my current code for filtering:
<?php
for ($index = 0; $index < count($fruits); $index++) { # sorry, but I hate foreach
if ($fruits[$index] == "apple")
unset($fruits[$index]);
}
?>
Thank you very much for your help!