<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >array_map</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="Arrays" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_keys" HREF="function.array-keys.html"><LINK REL="NEXT" TITLE="array_merge_recursive" HREF="function.array-merge-recursive.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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 do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-keys.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-merge-recursive.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-map" ></A >array_map</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10985" ></A ><P > (PHP 4 >= 4.0.6, PHP 5)</P >array_map -- Aplica uma função em todos os elementos dos arrays dados</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10988" ></A ><H2 >Descrição</H2 >array <B CLASS="methodname" >array_map</B > ( callback callback, array arr1 [, array ...] )<BR ></BR ><P > <B CLASS="function" >array_map()</B > retorna um array contendo todos os elementos de <CODE CLASS="parameter" >arr1</CODE > depois de aplicada uma determinada função em cada um. O número de parâmetros que esta função aceita deve coincidir com o número de arrays passados para a <B CLASS="function" >array_map()</B > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11007" ></A ><P ><B >Exemplo 1. Exemplo de <B CLASS="function" >array_map()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">cubo</font><font color="#007700">(</font><font color="#0000BB">$n</font><font color="#007700">)<br />{<br /> return </font><font color="#0000BB">$n</font><font color="#007700">*</font><font color="#0000BB">$n</font><font color="#007700">*</font><font color="#0000BB">$n</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$a </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">4</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">);<br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">"cubo"</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$b</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > E programa acima faz com que <CODE CLASS="varname" >$b</CODE > tenha: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11015" ></A ><P ><B >Exemplo 2. <B CLASS="function" >array_map()</B > - usando mais de um array</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">mostrar_Espanhol</font><font color="#007700">(</font><font color="#0000BB">$n</font><font color="#007700">, </font><font color="#0000BB">$m</font><font color="#007700">)<br />{<br /> return </font><font color="#DD0000">"O número $n é chamado de $m na Espanha"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">map_Espanhol</font><font color="#007700">(</font><font color="#0000BB">$n</font><font color="#007700">, </font><font color="#0000BB">$m</font><font color="#007700">)<br />{<br /> return array(</font><font color="#0000BB">$n </font><font color="#007700">=> </font><font color="#0000BB">$m</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$a </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">4</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">);<br /></font><font color="#0000BB">$b </font><font color="#007700">= array(</font><font color="#DD0000">"uno"</font><font color="#007700">, </font><font color="#DD0000">"dos"</font><font color="#007700">, </font><font color="#DD0000">"tres"</font><font color="#007700">, </font><font color="#DD0000">"cuatro"</font><font color="#007700">, </font><font color="#DD0000">"cinco"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">"mostrar_Espanhol"</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$c</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$d </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">"map_Espanhol"</font><font color="#007700">, </font><font color="#0000BB">$a </font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$d</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >// Saída de $c Array ( [0] => O número 1 é chamado de uno na Espanha [1] => O número 2 é chamado de dos na Espanha [2] => O número 3 é chamado de tres na Espanha [3] => O número 4 é chamado de cuatro na Espanha [4] => O número 5 é chamado de cinco na Espanha ) // Saída de $d Array ( [0] => Array ( [1] => uno ) [1] => Array ( [2] => dos ) [2] => Array ( [3] => tres ) [3] => Array ( [4] => cuatro ) [4] => Array ( [5] => cinco ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Normalmente quando se usa dois ou mais arrays, eles devem ter o mesmo tamanho porque a função callback é aplicada paralelamente nos elementos correpondentes. Se os arrays tem tamanhos diferentes, o menor array será extendido com elementos vazios. </P ><P > Uma forma interessante de se usar esta função é na construção de um array de arrays, o que pode ser facilmente feito usando <TT CLASS="constant" ><B >NULL</B ></TT > como o nome da função callback. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN11025" ></A ><P ><B >Exemplo 3. Criando um array de arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">4</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">);<br /></font><font color="#0000BB">$b </font><font color="#007700">= array(</font><font color="#DD0000">"um"</font><font color="#007700">, </font><font color="#DD0000">"dois"</font><font color="#007700">, </font><font color="#DD0000">"tres"</font><font color="#007700">, </font><font color="#DD0000">"quatro"</font><font color="#007700">, </font><font color="#DD0000">"cinco"</font><font color="#007700">);<br /></font><font color="#0000BB">$c </font><font color="#007700">= array(</font><font color="#DD0000">"uno"</font><font color="#007700">, </font><font color="#DD0000">"dos"</font><font color="#007700">, </font><font color="#DD0000">"tres"</font><font color="#007700">, </font><font color="#DD0000">"cuatro"</font><font color="#007700">, </font><font color="#DD0000">"cinco"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$d </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#0000BB">null</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">, </font><font color="#0000BB">$c</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$d</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Veja também <A HREF="function.array-filter.html" ><B CLASS="function" >array_filter()</B ></A >, <A HREF="function.array-reduce.html" ><B CLASS="function" >array_reduce()</B ></A > , <A HREF="function.array-walk.html" ><B CLASS="function" >array_walk()</B ></A >, <A HREF="function.create-function.html" ><B CLASS="function" >create_function()</B ></A >, e informações sobre o tipo <A HREF="language.pseudo-types.html#language.types.callback" >callback</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-keys.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array-merge-recursive.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_keys</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_merge_recursive</TD ></TR ></TABLE ></DIV ></BODY ></HTML >