Syntax:array array_filter ($array, $callback_function, $flag)
Parameters : the function takes three parameters, of which one is required and the other two are optional. $array(required): refers to the input array, for which the filtering operation should be performed. $callback_function(optional): refers to a user-defined function. If the function is not specified, then all array entries that are FALSE will be removed. $flag(optional): refers to the arguments passed to the callback function. - ARRAY_FILTER_USE_KEY - passes the key as the only argument to the callback function instead of the array value.
- ARRAY_FILTER_USE_BOTH - passes the value and key as arguments to the callback instead of the value.
Return value : The function returns a filtered array. Below is a program showing how to return or filter even elements from an array using the array_filter() function. php
// PHP function for checking even elements in an array
function
Even (
$array
)
{
// returns if the input integer is even
if
(
$array
% 2 == 0)
return
TRUE;
else
return
FALSE;
}
$array
=
array
( 12, 0, 0, 18, 27, 0, 46);
print_r (
array_filter
( $array
,
"Even"
));
?>
Output:Array ([ 0] = > 12 [1] = > 0 [2] = > 0 [3] = > 18 [5] = > 0 [6] = > 46)
B For this example, we will not pass a callback function and see the output. We will see that 0 or fake elements do not print: Php
// PHP function to check for even elements in an array
function
Even (
$array
)
{
// returns if the input is an integer even
if
( $array
% 2 == 0)
return
TRUE;
else
return
FALSE;
}
$array
=
array
( 12, 0, 0, 18, 27, 0, 46);
print_r (
array_filter
( $array
));
?>
Output:Array ([ 0] = > 12 [3] = > 18 [4] = > 27 [6] = > 46)
Link : http://php.net/manual/en/function.array-filter.php