Easy Tutorial
❮ Func String Strtr Func Cal Jdtounix ❯

PHP array_filter() Function

Complete PHP Array Reference Manual

Example

Filter elements of an array using a callback function:

<?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

The array_filter() function filters elements of an array using a callback function.

This function passes each value of the input array to the callback function. If the callback function returns true, the current value from the input array is returned into the result array. Array keys are preserved.


Syntax

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Parameter Description
array Required. Specifies the array to filter.
callback Optional. Specifies the callback function to use.
flag Optional. Determines what arguments are sent to callback: ARRAY_FILTER_USE_KEY - callback accepts the key as the only argument<br>ARRAY_FILTER_USE_BOTH - callback accepts both key and value

Technical Details

Return Value: Returns the filtered array.
PHP Version: 4.0.6+
--- ---

Complete PHP Array Reference Manual

❮ Func String Strtr Func Cal Jdtounix ❯