The array_filter () function uses the callback function to filter elements in the array.
[Function]
This function transmits each value in the specified array to the callback function in sequence.
If the callback function returns True, the current value of the specified array is included in the returned result array.
Note that the key name of the array remains unchanged.
Scope of use]
Php4> 4.0.6, php5.
[Use]
Array array_filter (array input [, callback])
Input/required/array for filtering
Callback/optional/specifies the callback function
[Example]
[Php]
<? Php
// Define the callback function
Function odd ($ var)
{
Return ($ var % 2 = 1 );
}
Function even ($ var)
{
Return ($ var % 2 = 0 );
}
// Define two arrays respectively
$ Array1 = array ("blue" => 6, "red" => 2, "green" => 3, "purple" => 4 );
$ Array2 = array ("green" => 5, "blue" => 6, "yellow" => 7, "cyan" => 8 );
Echo "filter odd numbers: \ n ";
Print_r (array_filter ($ array1, "odd "));
Echo "filter even numbers: \ n ";
Print_r (array_filter ($ array2, "even "));
/*
Filter odd values:
Array
(
[Green] => 3
)
Filter even numbers:
Array
(
[Blue] => 6
[Cyan] => 8
)
*/
Taken from zuodefeng's note