Array_filter and empty()

后端 未结 5 1502
梦毁少年i
梦毁少年i 2020-12-19 17:33

Warning: array_filter() expects parameter 2 to be a valid callback, function \'empty\' not found or invalid function name....

Why is e

5条回答
  •  借酒劲吻你
    2020-12-19 17:54

    See the documentation page on empty():

    Note: Because this is a language construct and not a function, it cannot be called using variable functions

    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:

    $arr = array_filter($arr, function($var){
        return empty($var);
    });
    

    In PHP < 5.3 you will need to create similar function first and then pass it to the array_filter().

    Did it help?

提交回复
热议问题