springo Posted April 12, 2007 Share Posted April 12, 2007 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! Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/ Share on other sites More sharing options...
Orio Posted April 12, 2007 Share Posted April 12, 2007 When you unset an element, it sets it to NULL. In the memory it still exists- the array takes places 0-6. In your case, you could just sort the array and it will give you the right result <?php for ($index = 0; $index < count($fruits); $index++) { # sorry, but I hate foreach if ($fruits[$index] == "apple") unset($fruits[$index]); } ksort($fruits); echo count($fruits); ?> Orio. Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227472 Share on other sites More sharing options...
Guest prozente Posted April 12, 2007 Share Posted April 12, 2007 <pre><?php $fruits [0] = "apple"; $fruits [1] = "lemon"; $fruits [2] = "orange"; $fruits [3] = "lemon"; $fruits [4] = "strawberry"; $fruits [5] = "apple"; $fruits [6] = "lemon"; for ($index = 0; $index < count($fruits); $index++) { # sorry, but I hate foreach if ($fruits[$index] == "apple") unset($fruits[$index]); } //reindex array $fruits = array_values($fruits); //show array after reindex print_r ($fruits); ?></pre> Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227473 Share on other sites More sharing options...
mjlogan Posted April 12, 2007 Share Posted April 12, 2007 for ($index = 0; $index < count($fruits); $index++) { # sorry, but I hate foreach to for ($index = 0, $count = count($fruits); $index < $count; $index++) { # sorry, but I hate foreach minor optimisation Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227497 Share on other sites More sharing options...
jitesh Posted April 12, 2007 Share Posted April 12, 2007 If you have not a long array $fruits [0] = "apple"; $fruits [1] = "lemon"; $fruits [2] = "orange"; $fruits [3] = "lemon"; $fruits [4] = "strawberry"; $fruits [5] = "apple"; $fruits [6] = "lemon"; want to delete "orange" $temp_array = array(); foreach($fruits as $key => $value){ if($value != "orange"){ $temp_array[] = $value; } } unset($fruits); $fruits = $temp_array; Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227522 Share on other sites More sharing options...
Orio Posted April 12, 2007 Share Posted April 12, 2007 I disagree jitesh. If has a long array, using another array in the same size will take lots of memory in the total. It's better to preform all of the actions on the same array. Orio. Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227560 Share on other sites More sharing options...
jitesh Posted April 12, 2007 Share Posted April 12, 2007 Orio, I have already mention at my top of the post "If you have not a long array". Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227579 Share on other sites More sharing options...
springo Posted April 12, 2007 Author Share Posted April 12, 2007 OK, thanks it works perfect now! Thanks for optimization tips too, I didn't realize loops executed the count() function each iteration Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227668 Share on other sites More sharing options...
boo_lolly Posted April 12, 2007 Share Posted April 12, 2007 it kept the key because you unset the element. you need to unset the key to remove it completely from the array. Link to comment https://forums.phpfreaks.com/topic/46694-cleaning-up-an-array/#findComment-227687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.