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.
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>';
}
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."
";
}
foreach($array as $key=>$value){
if ($value == ' ' || $value=='' || $value==' ' || empty($value)) { unset($array[$key]); }
echo 'key:'.$key.' value:'.$value."
";
}
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.