array_filter

(PHP 4 >= 4.0.6)

array_filter --  Filters elements of an array using a callback function

Description

array array_filter (array input [, mixed callback])

array_filter() returns an array containing all the elements of input filtered according a callback function. If the input is an associative array the keys are preserved.

Example 1. array_filter() example


function odd($var) {
    return ($var % 2 == 1);
}

function even($var) {
    return ($var % 2 == 0); 
}

$array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array (6, 7, 8, 9, 10, 11, 12);

$odd_arr = array_filter($array1, "odd");
$even_arr = array_filter($array2, "even");
      

This makes $odd_arr have array ("a"=>1, "c"=>3, "e"=>5);, and $even_arr have array (6, 8, 10, 12);,

See also array_map(), array_reduce().