Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does array_filter without a callback do?

Tags:

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 isn't any callback function defined here.

Confusing is also that the callback function is optional on the manual page.

like image 229
jdog Avatar asked Oct 19 '14 21:10

jdog


1 Answers

Removes empty or equivalent values from array:

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

Result

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

See the original documentation from the manual: Example #2 array_filter() without callback

like image 65
Rasclatt Avatar answered Oct 04 '22 21:10

Rasclatt