jdog jdog - 5 months ago 8
PHP Question

what does array_filter with no callback do?

I just came across this code:

array_filter( array_map('intval', $array) );


It seems to return all entries of $array converted to int where the number is > 0

However I can't see on the manual page that this is defined. It is supposed to return the array value if the callback function evaluates to true. But there is no callback function defined here.

Confusing is also that the callback function is optional on the manual page: http://php.net/manual/en/function.array-filter.php

Answer

Removes empty or equivalent values from array:

Example #2 array_filter() without callback

   $entry = array(
                 0 => 'foo',
                 1 => false,
                 2 => -1,
                 3 => null,
                 4 => ''
              );

    print_r(array_filter($entry));

Result

Array
(
    [0] => foo
    [2] => -1
)
Comments