2

Trying to remove empty values from array like this. It does miss one key each time i use unset(). I know there might be better way to complete task, but i need to know why current code is one missing some keys?

$values_arr = array(
   0 => "Text",
   1 => "",
   2 => "",
   3 => "Text",
   4 => "",
   5 => "Text"
);

Works in theory

for ($i = 0; $i < count($values_arr); $i++) {
    if ( empty($values_arr[$i]) ) {
        echo "<br> Blank key found " . $i . ", value was >" . $values_arr[$i] . "<";
        //Unset commented out
        //unset($values_arr[$i]);
    }
}

var_dump($values_arr);

Output

Blank key found 1, value was ><
Blank key found 2, value was ><
Blank key found 4, value was ><

array (size=6)
  0 => string 'Text' (length=4)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string 'Text' (length=4)
  4 => string '' (length=0)
  5 => string 'Text' (length=4)

Unset not working

for ($i = 0; $i < count($values_arr); $i++) {
    if ( empty($values_arr[$i]) ) {
        echo "<br> Blank key found " . $i . ", value was >" . $values_arr[$i] . "<";
        unset($values_arr[$i]);
    }
}

var_dump($values_arr);

Output

Blank key found 1, value was ><
Blank key found 2, value was ><

array (size=4)
  0 => string 'Text' (length=4)
  3 => string 'Text' (length=4)
  4 => string '' (length=0)
  5 => string 'Text' (length=4)

Why key 4 is not unset?

2 Answers 2

3

The problem with your code is that by the time you hit index 4 it will be equal to the size of your array and the loop stops.

There's a one-liner solution to this:

$values_arr = array_filter($values_arr, 'strlen');

It performs strlen() on each array element and returns a new array with elements that are not empty strings.

Another way is to find the keys corresponding to empty string values and then unset() those keys one by one in the main array:

foreach (array_keys($values_arr, '', true) as $key) {
    unset($values_arr[$key]);
}
Sign up to request clarification or add additional context in comments.

Comments

2

This should work for you:

<?php

    $values_arr = array(
                   0 => "Text",
                   1 => "",
                   2 => "",
                   3 => "Text",
                   4 => "",
                   5 => "Text"
                );

    foreach($values_arr as $k => $v) {

        if(empty($v) || $v == "")
            unset($values_arr[$k]);

    }

    print_r($values_arr);

?>

Output:

Array ( [0] => Text [3] => Text [5] => Text )


Why is your version not working?

Because in your for loop you have the condition: $i < count($values_arr)

So every iteration of the for loop it's going to check the condition! So if you unset a value in the array the count get's smaller! And after 2 unset's the for loop doesn't reach index 4 anymore!

3 Comments

Any idea why other one does not work i need to understand that more than find a working code.
@RomanToasov Your version isn't working because in your for loop you use count in the condition! So if you unset 2 index's it's not reaching index 4 and doesn't get unset! So you would have to assign the count value before you go into the for loop!
Brilliant! I forgot that middle expression is for() evaluates every loop not just once. "In the beginning of each iteration, expr2 is evaluated." php.net/manual/en/control-structures.for.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.