How I can remove all elements of an array that contain just whitespace, not whitespace in an element like "foobar "
but just empty array elements like " "
?
Thanks.
How I can remove all elements of an array that contain just whitespace, not whitespace in an element like "foobar "
but just empty array elements like " "
?
Thanks.
This code takes advantage of the callback parameter for
array_filter
. It will loop the array, calltrim()
on the value, and remove it if the resulting value evaluates tofalse
. (which an empty string will)$a = array_filter($a, 'trim');
preg_grep()
is your friend.CodePad.
This will drop all array members of which the string is blank or only consist of whitespace according to
\s
character class (spaces, tabs, and line breaks).Output
foreach($arr as $key=>$value)
{
unset($arr[$key]);
/* optional */
array_values($arr);Array when dumped then contains: