Jump to content

Cleaning up an array


springo

Recommended Posts

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

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

Guest prozente
<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

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

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.