<HTML ><HEAD ><TITLE >array_filter</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual de PHP" HREF="index.html"><LINK REL="UP" TITLE="Funciones de matrices" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_fill" HREF="function.array-fill.html"><LINK REL="NEXT" TITLE="array_flip" HREF="function.array-flip.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual de PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-fill.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array-flip.html" ACCESSKEY="N" >Siguiente</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-filter" ></A >array_filter</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN4886" ></A ><P > (PHP 4 >= 4.0.6)</P >array_filter -- Filters elements of an array using a callback function </DIV ><DIV CLASS="refsect1" ><A NAME="AEN4889" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_filter</B > ( array input [, callback function])<BR ></BR ><P > <B CLASS="function" >array_filter()</B > returns an array containing all the elements of <TT CLASS="parameter" ><I >input</I ></TT > filtered according a callback <TT CLASS="parameter" ><I >function</I ></TT >. If the <TT CLASS="parameter" ><I >input</I ></TT > is an associative array the keys are preserved. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4906" ></A ><P ><B >Ejemplo 1. <B CLASS="function" >array_filter()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >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); echo "Odd :\n"; print_r(array_filter($array1, "odd")); echo "Even:\n"; print_r(array_filter($array2, "even"));</PRE ></TD ></TR ></TABLE ><P > The printout of the program above will be: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Odd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P > Users may not change the array itself from the callback function. e.g. Add/delete an element, unset the array that <B CLASS="function" >array_filter()</B > is applied to. If the array is changed, the behavior of this function is undefined. </P ><P > See also <A HREF="function.array-map.html" ><B CLASS="function" >array_map()</B ></A > and <A HREF="function.array-reduce.html" ><B CLASS="function" >array_reduce()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.array-fill.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Inicio</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array-flip.html" ACCESSKEY="N" >Siguiente</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_fill</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Subir</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_flip</TD ></TR ></TABLE ></DIV ></BODY ></HTML >