Example
Use callback functionFilter elements in array:
<?php function test_odd($var) { return($var & 1); } $a1=array("a","b",2,3,4); print_r(array_filter($a1,"test_odd")); ?>
Definition and usage
array_filter() The function uses a callback function to filter the elements in the 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.
Syntax
array_filter(array,callbackfunction);
Parameters | Description |
array | Required. Specifies the array to filter. |
callbackfunction | Required. Specifies the callback function to be used. |
Technical details
Return value: | Returns the filtered array. |
PHP version: | 4.0.6+ |
$entry = array( 0 => '蓝色夏威夷的博客', 1 => false, 2 => 1, 3 => null, 4 => '', 5 => 'http://www.jb51.net', 6 => '0', 7 => array(), 8 => 0 ); $validarr = array_filter($entry); print_r($validarr); //输出结果: Array ( [0] => 蓝色夏威夷的博客 [2] => 1 [5] => http://www.jb51.net )
The above is the detailed content of PHP uses the callback function to filter the elements in the array function array_filter(). For more information, please follow other related articles on the PHP Chinese website!