viernes, 26 de enero de 2018

array_filter


array_filter — Filters elements of an array using a callback function

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.


function test_odd($var)
{
if($var==4){
return true;
}
}


$array2 = array(6, 7, 8, 9, 10, 11,4);
print_r(array_filter($array2, "test_odd"));


result
Array ( [6] => 4 )

because the function returned true, if not will return empty array

No hay comentarios:

Publicar un comentario