Array_filter和empty() [英] Array_filter and empty()

查看:165
本文介绍了Array_filter和empty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


警告:array_filter()期望参数2是有效的回调,
函数空未找到或无效的函数名...。

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');

这有效: if(empty($ arr ['foo']) )die();

推荐答案

答案



empty() 不是函数,而是语言构造, array_filter()只能接受函数作为其回调。

Answer

empty() is not a function but a language construct and array_filter() can only accept a function as its callback.

这是手册页上的一个小注释:

This is given as a small note on the manual page:


注意:因为这是一种语言构造而不是函数,它不能使用可变函数

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



解决方法



要解决此问题,您可以将其中的空白包装另一个函数例如:

Work around

To work around this you can wrap empty in another function for example:

function empty_test($val) {
    return empty($val);
}

然后这样称呼它:

$arr = array_filter($arr, 'empty_test');

这篇关于Array_filter和empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆