Current Location: Home> Function Categories> array_filter

array_filter

Use callback function to filter units in array
Name:array_filter
Category:Array
Programming Language:php
One-line Description:Use a callback function to filter elements in an array.

Definition and usage

array_filter() function uses a callback function to filter values ​​in an array.

This function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. The array key names remain unchanged.

Example

Use callback functions to filter elements in an array:

 <?php
function test_odd ( $var )
{
return ( $var & 1 ) ;
}

$a1 = array ( "a" , "b" , 2 , 3 , 4 ) ;
print_r ( array_filter ( $a1 , "test_odd" ) ) ;
?>

Try it yourself

grammar

 array_filter ( array , callbackfunction ) ;
parameter describe
array Required. Specifies the array to be filtered.
callbackfunction Required. Specifies the callback function to be used.