0

I seem to be hitting a wall when it comes to removing empty values in my array.

I have the following array:

key:0 value: 

key:1 value:type

key:3 value:gear

key:4 value: 

key:5 value:rarity

As you can see, Key 0, and Key 4 are both empty (or filled with a space).

I have tried the following, but it doesn't appear to have worked:

foreach($array as $key=>$value){
    if ($value == ' ' || $value=='' || $value==' ' || empty($value)) { unset($array[$key]); }
    echo 'key:'.$key.' value:'.$value.'</br></br>';
}

and even tried it in 2 foreach loops just to be sure, but still nothing.

5
  • 4
    Why no array_filter ? Commented Nov 21, 2017 at 21:40
  • is there an echo in here? lol! edit: not anymore. Commented Nov 21, 2017 at 21:41
  • @Fred-ii- ninja edit ftw ;) Commented Nov 21, 2017 at 21:41
  • @castis lmho! Commented Nov 21, 2017 at 21:41
  • Sorry, I forgot to put in the question, I tried it but it didn't work either, unless I did it wrong? I tried array_filter($array) and also array_filter($array, ' ') Commented Nov 21, 2017 at 21:42

2 Answers 2

1

There are two issues: First you're not calling continue even if you unset the array key, meaning that you still print the value. Second - you can easily use trim to remove any whitespace that would fubar the comparison.

foreach ($array as $key => $value) {
    if (empty($value) || !trim($value)) {
        unset($array[$key]);
        continue;
    }

    echo 'key:' . $key . ' value:' . $value . '</br></br>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

You sir, deserve a pint of the finest beer. That has worked. Thank you very much. It left the first value a empty, but the rest have gone... Why would that happen?
0

Let's not just rush to the "just do this" answer. Here is what you tried:

Code: (Demo)

$array=[0=>'',1=>'type',3=>'gear',4=>' ',5=>'rarity'];
foreach($array as $key=>$value){
    if ($value == ' ' || $value=='' || $value==' ' || empty($value)) { unset($array[$key]); }
    echo 'key:'.$key.' value:'.$value."\n";
}
foreach($array as $key=>$value){
    if ($value == ' ' || $value=='' || $value==' ' || empty($value)) { unset($array[$key]); }
    echo 'key:'.$key.' value:'.$value."\n";
}

It is doing what you asked it to do. The first loop unsets the unwanted elements AND prints the keys & values assigned to the "copy" of $array that foreach() "writes" & iterates. The second loop writes & iterates a new "copy" of $array (which was previously modified by the first loop) and you can see that the empty-ish elements are now gone.

Output:

key:0 value:        # unset $array[$key] and display $value (these are different entities
key:1 value:type
key:3 value:gear
key:4 value: 
key:5 value:rarity
key:1 value:type    # you see elements [0] and [4] are removed as intended
key:3 value:gear
key:5 value:rarity

Now, for the "Do this" part... You have several options. Here are a few:

Code: (Demo)

$array=[0=>'',1=>'type',3=>'gear',4=>' ',5=>'rarity'];

var_export(array_filter($array,function($v){return strlen(trim($v));}));  // professional grade: uses the function specifically designed to perform this function and the process will be instantly understood by future devs that read your code

var_export(preg_grep('/\S/',$array));  // A regular expression filter. Regex solutions should only be used when other methods will not suffice.  For this reason, this method is ill-advised

var_export(preg_grep('/^\s*$/',$array,PREG_GREP_INVERT));  // A regular expression filter (inverted pattern filteration). Regex solutions should only be used when other methods will not suffice.  For this reason, this method is ill-advised

foreach($array as $k=>$v){                    // possibly the fastest method, I didn't test, but it is also the most verbose and less descriptive as a technique
    if(!strlen(trim($v))){unset($array[$k]);}
}
var_export($array);

Output:

array (
  1 => 'type',
  3 => 'gear',
  5 => 'rarity',
)array (
  1 => 'type',
  3 => 'gear',
  5 => 'rarity',
)array (
  1 => 'type',
  3 => 'gear',
  5 => 'rarity',
)array (
  1 => 'type',
  3 => 'gear',
  5 => 'rarity',
)

Now the most important piece of advice to give you from the manual:

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array()
  • (an empty array)
  • $var; (a variable declared, but without a value)

This "greedy" default behavior is used by empty() and array_filter() and can really throw a wrench into your task if you are unaware. This is why in my suggested methods, I write a custom filter condition and in the foreach loop, I replace empty() with a strlen() call.

1 Comment

@DisplayName ...actually, what are you doing in your project? Do you need to modify the array or just not echo certain elements? I am assuming that you are wanting to modify the array and that you are only echoing to demonstrate your failed method. If you are merely echoing the keys and values, then unset() is unnecessary.

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.