mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-24 07:02:09 +01:00
fixed svn properties git-svn-id: https://svn.php.net/repository/phpdoc/pt_BR/trunk@283922 c90b9560-bf6c-de11-be94-00142212c4b1
350 lines
7.2 KiB
XML
350 lines
7.2 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- EN-Revision: n/a Maintainer: felipe Status: ready --><!-- CREDITS: lucasr -->
|
|
<refentry xml:id="function.array-map" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>array_map</refname>
|
|
<refpurpose>Aplica uma função em todos os elementos dos arrays dados</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>array</type><methodname>array_map</methodname>
|
|
<methodparam><type>callback</type><parameter>callback</parameter></methodparam>
|
|
<methodparam><type>array</type><parameter>arr1</parameter></methodparam>
|
|
<methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<function>array_map</function> retorna um array contendo todos
|
|
os elementos de <parameter>arr1</parameter> 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
|
|
<function>array_map</function>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>callback</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Função callback para executar para cada elementos dos arrays.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>arr1</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Um array para percorrer chamando função <parameter>callback</parameter>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Variável lista de argumentos array para percorrer chamando a função
|
|
<parameter>callback</parameter>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Retorna um array contendo todos elementos de <parameter>arr1</parameter>
|
|
depois de ter aplicado a função <parameter>callback</parameter> para cada um.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Exemplo de <function>array_map</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function cubo($n)
|
|
{
|
|
return $n*$n*$n;
|
|
}
|
|
|
|
$a = array(1, 2, 3, 4, 5);
|
|
$b = array_map("cubo", $a);
|
|
print_r($b);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
E programa acima faz com que <varname>$b</varname> tenha:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => 1
|
|
[1] => 8
|
|
[2] => 27
|
|
[3] => 64
|
|
[4] => 125
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>array_map</function> - usando mais de um array</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function mostrar_Espanhol($n, $m)
|
|
{
|
|
return "O número $n é chamado de $m na Espanha";
|
|
}
|
|
|
|
function map_Espanhol($n, $m)
|
|
{
|
|
return array($n => $m);
|
|
}
|
|
|
|
$a = array(1, 2, 3, 4, 5);
|
|
$b = array("uno", "dos", "tres", "cuatro", "cinco");
|
|
|
|
$c = array_map("mostrar_Espanhol", $a, $b);
|
|
print_r($c);
|
|
|
|
$d = array_map("map_Espanhol", $a , $b);
|
|
print_r($d);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
// 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
|
|
)
|
|
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
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.
|
|
</para>
|
|
<para>
|
|
Uma forma interessante de se usar esta função é na construção de um array de
|
|
arrays, o que pode ser facilmente feito usando &null;
|
|
como o nome da função callback.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Criando um array de arrays</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = array(1, 2, 3, 4, 5);
|
|
$b = array("um", "dois", "tres", "quatro", "cinco");
|
|
$c = array("uno", "dos", "tres", "cuatro", "cinco");
|
|
|
|
$d = array_map(null, $a, $b, $c);
|
|
print_r($d);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
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
|
|
)
|
|
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
Se o array do argumento contém chaves string, então o array retornado
|
|
conterá chaves string se e somente se exatamente um array é passado. Se
|
|
mais que um argumento é passado, então o array retornado sempre terá
|
|
chaves inteiras.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>array_map</function> - com chaves string</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$arr = array("stringkey" => "value");
|
|
function cb1($a) {
|
|
return array ($a);
|
|
}
|
|
function cb2($a, $b) {
|
|
return array ($a, $b);
|
|
}
|
|
var_dump(array_map("cb1", $arr));
|
|
var_dump(array_map("cb2", $arr, $arr));
|
|
var_dump(array_map(null, $arr));
|
|
var_dump(array_map(null, $arr, $arr));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
array(1) {
|
|
["stringkey"]=>
|
|
array(1) {
|
|
[0]=>
|
|
string(5) "value"
|
|
}
|
|
}
|
|
array(1) {
|
|
[0]=>
|
|
array(2) {
|
|
[0]=>
|
|
string(5) "value"
|
|
[1]=>
|
|
string(5) "value"
|
|
}
|
|
}
|
|
array(1) {
|
|
["stringkey"]=>
|
|
string(5) "value"
|
|
}
|
|
array(1) {
|
|
[0]=>
|
|
array(2) {
|
|
[0]=>
|
|
string(5) "value"
|
|
[1]=>
|
|
string(5) "value"
|
|
}
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>array_filter</function></member>
|
|
<member><function>array_reduce</function></member>
|
|
<member><function>array_walk</function></member>
|
|
<member><function>create_function</function></member>
|
|
</simplelist>
|
|
&seealso.callback;
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"../../../../manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|