1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 15:12:20 +01:00
Files
archived-doc-zh/reference/array/functions/array-map.xml
魔王卷子 74e0b14a2b 更新 array 部分 part 4 (#186)
* 更新 array 部分 part 4
2022-01-31 04:59:26 +08:00

460 lines
9.7 KiB
XML
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- $Author$ -->
<!-- EN-Revision: 31cacb6f262f455c616094cfe6d09bc0d1df2748 Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<refentry xml:id="function.array-map" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_map</refname>
<refpurpose>
为数组的每个元素应用回调函数
</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> 返回一个 &array;,内容为 <parameter>array</parameter> 的元素按相应的顺序调用
<parameter>callback</parameter> 后的结果(如果提供了更多数组,还会利用 <parameter>arrays</parameter>
传入)。<parameter>callback</parameter> 函数形参的数量必须匹配 <function>array_map</function>
实参中数组的数量。多余的实参数组将会被忽略。如果提供的实参数组的数量不足,将抛出 <classname>ArgumentCountError</classname>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>callback</parameter></term>
<listitem>
<para>
回调函数 <type>callable</type>,应用到每个数组里的每个元素。
</para>
<para>
多个数组操作合并时,<parameter>callback</parameter> 可以设置为 &null;
如果只提供了 <parameter>array</parameter> 一个数组,
<methodname>array_map</methodname> 会返回输入的数组。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<para>
数组,遍历运行 <parameter>callback</parameter> 函数。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>arrays</parameter></term>
<listitem>
<para>
额外的数组列表,每个都遍历运行 <parameter>callback</parameter> 函数。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回数组,包含 <parameter>callback</parameter> 函数处理之后 <parameter>array</parameter> (有多个数组时,为 <parameter>arrays</parameter>) 对应索引所有元素作为函数的参数。
</para>
<para>
当仅仅传入一个数组时返回的数组会保留传入参数的键key
传入多个数组时,返回的数组键是按顺序的 integer。
</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><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>
这使得 <varname>$b</varname> 成为:
</para>
<screen>
<![CDATA[
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>array_map</function> 使用匿名函数</title>
<programlisting role="php">
<![CDATA[
<?php
$func = function(int $value): int {
return $value * 2;
};
print_r(array_map($func, range(1, 5)));
// 或者从 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>:使用更多的数组</title>
<programlisting role="php">
<![CDATA[
<?php
function show_Spanish(int $n, string $m): string
{
return "The number {$n} is called {$m} in Spanish";
}
function map_Spanish(int $n, string $m): array
{
return [$n => $m];
}
$a = [1, 2, 3, 4, 5];
$b = ['uno', 'dos', 'tres', 'cuatro', 'cinco'];
$c = array_map('show_Spanish', $a, $b);
print_r($c);
$d = array_map('map_Spanish', $a , $b);
print_r($d);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
// 打印 $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
// 打印 $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>
传入两个及以上的数组时,它们元素数量将会相同。因为回调函数会并行地处理相互对应的元素。
如果几个数组的元素数量不一致:空元素会扩展短那个数组,直到长度和最长的数组一样。
</para>
<para>
此函数有个有趣的用法:传入 &null; 作为回调函数的名称,将创建多维数组(一个数组,内部包含数组。)
</para>
<para>
<example>
<title>多个数组的合并操作</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>array1</parameter> 时,<parameter>callback</parameter> 设置为 &null;
</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>key是 string</title>
<programlisting role="php">
<![CDATA[
<?php
$arr = array("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> - 关联数组</title>
<para>
虽然 <function>array_map</function> 不能直接支持使用数组的键key作为输入但可以使用
<function>array_keys</function> 进行模拟。
</para>
<programlisting role="php">
<![CDATA[
<?php
$arr = [
'v1' => 'First release',
'v2' => 'Second release',
'v3' => 'Third release',
];
// 注意: 在 7.4.0 之前,使用较长的语法来代替匿名函数。
$callback = fn(string $k, string $v): string => "$k was the $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 was the First release"
[1]=>
string(25) "v2 was the Second release"
[2]=>
string(24) "v3 was the Third release"
}
]]>
</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
-->