Warning: array_filter() expects parameter 2 to be a valid callback, function 'empty' not found or invalid function name....
Why is empty considered a invalid callback?
$arr = array_filter($arr, 'empty');
This works: if(empty($arr['foo'])) die();
empty()
is a language construct, and not a true function in terms of PHP, so you can't pass its name as an argument to functions likearray_filter()
andcall_user_func_array()
.From the manual:
For a workaround, just wrap it in another user-defined function; see Treffynnon's answer.
You can use just array_filter() function without callback:
Remove empty array elements in PHP
Result:
Answer
empty()
is not a function but a language construct andarray_filter()
can only accept a function as its callback.This is given as a small note on the manual page:
Work around
To work around this you can wrap empty in another function for example:
And then call it like so:
See the documentation page on
empty()
:So basically
empty()
is not a function, and because callback must be a function,empty()
can not be passed as callback.But you can create callback that may use
empty()
. The following should work in PHP > 5.3:In PHP < 5.3 you will need to create similar function first and then pass it to the
array_filter()
.Did it help?