Use a callback function 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 " ) ) ; ?>The array_filter() function uses a callback function to filter elements 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. Array key names remain unchanged.
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
parameter | describe |
---|---|
array | Required. Specifies the array to filter. |
callback | Optional. Specifies the callback function to be used. |
flag | Optional. Determine the parameter form received by the callback: ARRAY_FILTER_USE_KEY - callback accepts key name as the only parameter ARRAY_FILTER_USE_BOTH - callback accepts both key name and key value |
Return value: | Returns the filtered array. |
---|---|
PHP version: | 4.0.6+ |