foreach($array as $key => $value)
if (strlen($value) == 0)
unset($array[$key]);
theres so many built-in array functions so is there one that does this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I suppose you could use
array_filter, to do that kind of thing (quoting) :And :
For instance, using something like this portion of code :
You’d get the following output :
I didn’t used a callback function, here — which means that all "empty" values have been removed ;; the tricky part being that if I had en entry with
0as its value, it would have been removed too…Depending on your situation, that would be acceptable or not… So maybe you’d prefer to use a specific callback function to do exactly what you want, and have a better control over what gets filtered out ?
And here’s an example with a callback function :
(Note the
'a' => 0line in the array)And we now get :
i.e. using a callback function allowed us to specify more clearly what should be filtered out (here, lines containing exactly an empty string, without type conversion).