Some elements in my array are empty strings based on what the user has submitted. I need to remove those elements. I have this:
foreach($linksArray as $link)
{
if($link == '')
{
unset($link);
}
}
print_r($linksArray);
But it doesn't work, $linksArray
still has empty elements. I have also tried doing it with the empty()
function but the outcome is the same.
You can just do
array_filter: "If no callback is supplied, all entries of input equal to FALSE will be removed." This means that elements with values NULL, 0, '0', '', FALSE, array() will be removed too.
The other option is doing
which will remove elements with values NULL, '' and FALSE.
Hope this helps :)
UPDATE
Here is an example.
To sum up:
For multidimensional array
output
Just one line : Update (thanks to @suther):
Another one liner to remove empty ("" empty string) elements from your array.
Note: This code deliberately keeps null, 0 and false elements.
Or maybe you want to trim your array elements first:
Note: This code also removes null and false elements.