Latest web development tutorials

PHP array_filter () function

PHP Array Reference Complete PHP Array Reference

Examples

Using a callback function filter element in the array:

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

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

Running instance »

Definition and Usage

array_filter () function filter elements in an array using a callback function.

The function of the input array for each key value passed to the callback function. If the callback function returns true, put the current key input array is returned to the resulting array. Array key name unchanged.


grammar

array_filter( array,callbackfunction );

参数 描述
array 必需。规定要过滤的数组。
callbackfunction 必需。规定要用的回调函数。

technical details

return value: Returns the filtered array.
PHP version: 4.0.6+


PHP Array Reference Complete PHP Array Reference