mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-24 07:02:09 +01:00
474 lines
10 KiB
XML
474 lines
10 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 33968dfebb9b847733d02ee221b3b8054a101b41 Maintainer: lhsazevedo Status: ready --><!-- CREDITS: felipe,lucasr,fabioluciano,ae,lhsazevedo,ABDALAZARD,leonardolara -->
|
|
<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 class="union"><type>callable</type><type>null</type></type><parameter>callback</parameter></methodparam>
|
|
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
|
<methodparam rep="repeat"><type>array</type><parameter>arrays</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<function>array_map</function> retorna um &array; contendo
|
|
os resultados da aplicação do <parameter>callback</parameter>
|
|
ao valor correspondente do <parameter>array</parameter>
|
|
(e <parameter>arrays</parameter> se mais arrays forem fornecidos)
|
|
usados como argumentos para a função de callback.
|
|
O número de parâmetros que a função <parameter>callback</parameter>
|
|
aceita deve coincidir com o número de arrays
|
|
passados para <function>array_map</function>. Arrays
|
|
de entrada excedentes são ignorados. Um <classname>ArgumentCountError</classname>
|
|
é lançado se um número insuficiente de argumentos é fornecido.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>callback</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Um <type>callable</type> para executar para cada elemento em cada array.
|
|
</para>
|
|
<para>
|
|
&null; pode ser passado como um valor para <parameter>callback</parameter>
|
|
para realizar uma operação zip em múltiplos arrays e retornar um array
|
|
onde cada elemento é um array contendo os elementos dos arrays de entrada do mesmo índice (veja o exemplo abaixo).
|
|
Se apenas <parameter>array</parameter> for fornecido,
|
|
<methodname>array_map</methodname> retornará o array de entrada.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Um array para percorrer chamando função <parameter>callback</parameter>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>arrays</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Lista variável suplementar de argumentos array para passar pela
|
|
função <parameter>callback</parameter>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Retorna um array contendo os resultados da aplicação da função <parameter>callback</parameter>
|
|
ao valor correspondente do <parameter>array</parameter>
|
|
(e <parameter>arrays</parameter> se mais arrays forem fornecidos)
|
|
usados como argumentos para o callback.
|
|
</para>
|
|
<para>
|
|
O array retornado irá preservar as chaves do argumento array se e somente
|
|
se exatamente um array for passado. Se mais de um array for passado, o
|
|
array retornado terá chaves inteiras sequenciais.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
&array.changelog.by-ref;
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Exemplo da função <function>array_map</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function cube($n)
|
|
{
|
|
return ($n * $n * $n);
|
|
}
|
|
|
|
$a = [1, 2, 3, 4, 5];
|
|
$b = array_map('cube', $a);
|
|
print_r($b);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Faz com que <varname>$b</varname> tenha:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => 1
|
|
[1] => 8
|
|
[2] => 27
|
|
[3] => 64
|
|
[4] => 125
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>array_map</function> utilizando uma função lambda</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$func = function(int $value): int {
|
|
return $value * 2;
|
|
};
|
|
|
|
print_r(array_map($func, range(1, 5)));
|
|
|
|
// Ou a partir do PHP 7.4.0:
|
|
|
|
print_r(array_map(fn($value): int => $value * 2, range(1, 5)));
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<screen>
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => 2
|
|
[1] => 4
|
|
[2] => 6
|
|
[3] => 8
|
|
[4] => 10
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>array_map</function> - usando mais de um array</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function mostrar_Espanhol(int $n, string $m): string
|
|
{
|
|
return "O número {$n} é chamado de {$m} em espanhol";
|
|
}
|
|
|
|
function map_Espanhol(int $n, string $m): array
|
|
{
|
|
return [$n => $m];
|
|
}
|
|
|
|
$a = [1, 2, 3, 4, 5];
|
|
$b = ['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 em espanhol
|
|
[1] => O número 2 é chamado de dos em espanhol
|
|
[2] => O número 3 é chamado de tres em espanhol
|
|
[3] => O número 4 é chamado de cuatro em espanhol
|
|
[4] => O número 5 é chamado de cinco em espanhol
|
|
)
|
|
|
|
// 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>Realizando uma operação zip de arrays</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = [1, 2, 3, 4, 5];
|
|
$b = ['one', 'two', 'three', 'four', 'five'];
|
|
$c = ['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>
|
|
<example>
|
|
<title>
|
|
<parameter>callback</parameter> &null; apenas com
|
|
<parameter>array</parameter>
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$array = [1, 2, 3];
|
|
var_dump(array_map(null, $array));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
array(3) {
|
|
[0]=>
|
|
int(1)
|
|
[1]=>
|
|
int(2)
|
|
[2]=>
|
|
int(3)
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>array_map</function> - com chaves string</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$arr = ['stringkey' => 'value'];
|
|
function cb1($a) {
|
|
return [$a];
|
|
}
|
|
function cb2($a, $b) {
|
|
return [$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>
|
|
<example>
|
|
<title><function>array_map</function> - arrays associativos</title>
|
|
<para>
|
|
Enquanto <function>array_map</function> não suporta diretamente
|
|
o uso da chave do array como entrada, isso pode ser simulado usando <function>array_keys</function>.
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$arr = [
|
|
'v1' => 'Primeiro lançamento',
|
|
'v2' => 'Segundo lançamento',
|
|
'v3' => 'Terceiro lançamento',
|
|
];
|
|
|
|
// Nota: Antes da 7.4.0, use a sintaxe mais longa para funções anônimas.
|
|
$callback = fn(string $k, string $v): string => "$k foi o $v";
|
|
|
|
$result = array_map($callback, array_keys($arr), array_values($arr));
|
|
|
|
var_dump($result);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
array(3) {
|
|
[0]=>
|
|
string(24) "v1 foi o Primeiro lançamento"
|
|
[1]=>
|
|
string(25) "v2 foi o Segundo lançamento"
|
|
[2]=>
|
|
string(24) "v3 foi o Terceiro lançamento"
|
|
}
|
|
]]>
|
|
</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>
|
|
</simplelist>
|
|
</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:"~/.phpdoc/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
|
|
-->
|