Search code examples
phpcallbackarray-map

empty() not a valid callback?


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.


Solution

  • 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