empty() 不是一个有效的回调?

发布于 08-28 04:13 字数 768 浏览 22 评论 0原文

我正在尝试在 php.ini 的数组映射中使用empty()。我收到错误消息,表明这不是有效的回调。

$ cat test.php
<?

$arrays = array(
   'arrEmpty' => array(
        '','',''
    ),
);

foreach ( $arrays as $key => $array ) {

        echo $key . "\n";
        echo array_reduce( $array, "empty" );
        var_dump( array_map("empty", $array) );
        echo "\n\n";

}

$ php test.php
arrEmpty

Warning: array_reduce(): The second argument, 'empty', should be a valid callback in /var/www/authentication_class/test.php on line 12

Warning: array_map(): The first argument, 'empty', should be either NULL or a valid callback in /var/www/authentication_class/test.php on line 13
NULL

这不应该起作用吗?

长话短说:我试图变得(太?)聪明并检查所有数组值都不是空字符串。

I'm trying to use empty() in array mapping in php. I'm getting errors that it's not a valid callback.

$ cat test.php
<?

$arrays = array(
   'arrEmpty' => array(
        '','',''
    ),
);

foreach ( $arrays as $key => $array ) {

        echo $key . "\n";
        echo array_reduce( $array, "empty" );
        var_dump( array_map("empty", $array) );
        echo "\n\n";

}

$ php test.php
arrEmpty

Warning: array_reduce(): The second argument, 'empty', should be a valid callback in /var/www/authentication_class/test.php on line 12

Warning: array_map(): The first argument, 'empty', should be either NULL or a valid callback in /var/www/authentication_class/test.php on line 13
NULL

Shouldn't this work?

Long story: I'm trying to be (too?) clever and checking that all array values are not empty strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

是伱的2024-09-04 04:13:14

这是因为 empty 是一种语言构造,而不是一个函数。来自 empty() 手册:

注意:因为这是一个语言构造而不是一个函数,所以不能使用变量函数调用它

It's because empty is a language construct and not a function. From the manual on empty():

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

删除→记忆2024-09-04 04:13:14

尝试 array_filter 不带回调反而:

如果未提供回调,则所有等于 FALSE 的输入条目(请参阅转换为布尔值)将被删除。

然后,您可以使用 count(array_filter($array)) 来查看它是否仍然有值。

或者简单地将空包装到可调用中,如下所示:

array_reduce($array, create_function('$x', 'return empty($x);'));

或从 PHP 5.3 开始

array_reduce($array, function($x) { return empty($x); });

Try array_filter with no callback instead:

If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.

You can then use count(array_filter($array)) to see if it still has values.

Or simply wrap empty into a callable, like this:

array_reduce($array, create_function('$x', 'return empty($x);'));

or as of PHP 5.3

array_reduce($array, function($x) { return empty($x); });
灰色世界里的红玫瑰2024-09-04 04:13:14

为了添加到其他函数中,PHP 开发人员通常创建如下函数:

function isEmpty($var)
{
    return empty($var);
}

To add to the others, it's common for PHP developers to create a function like this:

function isEmpty($var)
{
    return empty($var);
}
绿光2024-09-04 04:13:14

Empty不能用作回调,它需要对变量进行操作。来自手册

注意:empty() 仅检查变量,因为其他任何内容都会导致解析错误。换句话说,以下内容将不起作用:empty(trim($name))。

Empty can't be used as a callback, it needs to operate on a variable. From the manual:

Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

淡淡绿茶香2024-09-04 04:13:14

我不知道为什么,不知何故,empty() 在回调中为我工作。

我最初收到此错误的原因是因为我试图作为独立函数进行回调,而它在我的类中,我必须使用 array(&$this ,'func_name') 来调用它,

请参阅下面的代码。这对我有用。我是 php 5.2.8,如果这很重要的话......

$table_values = array_filter( $table_values, array(&$this, 'remove_blank_rows') );

function remove_blank_rows($row){


        $not_blank = false;

        foreach($row as $col){
            $cell_value = trim($col);
            if(!empty( $cell_value )) {
                $not_blank = true;
                break;
            }
        }

        return $not_blank;

}

I don't know why, somehow empty() worked for me inside a callback.

The reason why I was originally getting this error was because I was trying to callback as an independent function, whereas it was inside my class and I had to call it using array(&$this ,'func_name')

See code below. It works for me. I am php 5.2.8, if that matters...

$table_values = array_filter( $table_values, array(&$this, 'remove_blank_rows') );

function remove_blank_rows($row){


        $not_blank = false;

        foreach($row as $col){
            $cell_value = trim($col);
            if(!empty( $cell_value )) {
                $not_blank = true;
                break;
            }
        }

        return $not_blank;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文