Some elements in my array are empty strings from users. $linksArray
still has empty elements after the following:
foreach($linksArray as $link)
{
if($link == '')
{
unset($link);
}
}
print_r($linksArray);
The empty()
function doesn't work either.
Best Answer
As you're dealing with an array of strings, you can simply use
array_filter()
, which conveniently handles all this for you:Keep in mind that if no callback is supplied, all entries of array equal to
FALSE
(see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact string'0'
, you will need a custom callback:Note: If you need to reindex the array after removing the empty elements, use: