2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:30:45 +00:00
|
|
|
<!-- $Revision$ -->
|
2020-12-04 00:35:15 +00:00
|
|
|
<refentry xml:id="function.array-map" xmlns="http://docbook.org/ns/docbook">
|
2006-10-31 11:24:02 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>array_map</refname>
|
|
|
|
<refpurpose>Applies the callback to the elements of the given arrays</refpurpose>
|
|
|
|
</refnamediv>
|
2007-06-18 22:49:15 +00:00
|
|
|
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2006-10-31 11:24:02 +00:00
|
|
|
<methodsynopsis>
|
|
|
|
<type>array</type><methodname>array_map</methodname>
|
2020-12-04 00:35:15 +00:00
|
|
|
<methodparam><type class="union"><type>callable</type><type>null</type></type><parameter>callback</parameter></methodparam>
|
2020-11-02 11:17:26 +00:00
|
|
|
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
|
|
|
<methodparam rep="repeat"><type>array</type><parameter>arrays</parameter></methodparam>
|
2006-10-31 11:24:02 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2019-05-16 21:16:35 +00:00
|
|
|
<function>array_map</function> returns an &array; containing
|
2019-04-01 19:15:50 +00:00
|
|
|
the results of applying the <parameter>callback</parameter>
|
2020-11-02 11:17:26 +00:00
|
|
|
to the corresponding index of <parameter>array</parameter>
|
|
|
|
(and <parameter>arrays</parameter> if more arrays are provided)
|
2019-04-01 19:15:50 +00:00
|
|
|
used as arguments for the callback.
|
2006-10-31 11:24:02 +00:00
|
|
|
The number of parameters that the <parameter>callback</parameter>
|
2019-04-01 19:15:50 +00:00
|
|
|
function accepts should match the number of arrays
|
|
|
|
passed to <function>array_map</function>.
|
2006-10-31 11:24:02 +00:00
|
|
|
</para>
|
2007-06-18 22:49:15 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>callback</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2020-09-02 16:31:49 +00:00
|
|
|
A <type>callable</type> to run for each element in each array.
|
2007-06-18 22:49:15 +00:00
|
|
|
</para>
|
2019-05-21 19:56:39 +00:00
|
|
|
<para>
|
|
|
|
&null; can be passed as a value to <parameter>callback</parameter>
|
|
|
|
to perform a zip operation on multiple arrays.
|
2020-11-02 11:17:26 +00:00
|
|
|
If only <parameter>array</parameter> is provided,
|
2019-05-21 19:56:39 +00:00
|
|
|
<methodname>array_map</methodname> will return the input array.
|
|
|
|
</para>
|
2007-06-18 22:49:15 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2020-11-02 11:17:26 +00:00
|
|
|
<term><parameter>array</parameter></term>
|
2007-06-18 22:49:15 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
An array to run through the <parameter>callback</parameter> function.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2020-11-02 11:17:26 +00:00
|
|
|
<term><parameter>arrays</parameter></term>
|
2007-06-18 22:49:15 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2019-04-01 19:15:50 +00:00
|
|
|
Supplementary variable list of array arguments to run through the
|
2007-06-18 22:49:15 +00:00
|
|
|
<parameter>callback</parameter> function.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
2019-04-01 19:15:50 +00:00
|
|
|
Returns an array containing the results of applying the <parameter>callback</parameter>
|
2020-11-02 11:17:26 +00:00
|
|
|
function to the corresponding index of <parameter>array</parameter>
|
|
|
|
(and <parameter>arrays</parameter> if more arrays are provided)
|
2019-04-01 19:15:50 +00:00
|
|
|
used as arguments for the callback.
|
2007-06-18 22:49:15 +00:00
|
|
|
</para>
|
2019-12-08 15:18:49 +00:00
|
|
|
<para>
|
|
|
|
The returned array will preserve the keys of the array argument if and only
|
|
|
|
if exactly one array is passed. If more than one array is passed, the
|
|
|
|
returned array will have sequential integer keys.
|
|
|
|
</para>
|
2007-06-18 22:49:15 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
2006-10-31 11:24:02 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_map</function> example</title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2002-11-11 00:36:16 +00:00
|
|
|
<?php
|
2006-10-31 11:24:02 +00:00
|
|
|
function cube($n)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2019-05-15 11:39:08 +00:00
|
|
|
return ($n * $n * $n);
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 11:39:08 +00:00
|
|
|
$a = [1, 2, 3, 4, 5];
|
|
|
|
$b = array_map('cube', $a);
|
2002-04-15 00:12:54 +00:00
|
|
|
print_r($b);
|
2002-11-11 00:36:16 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
This makes <varname>$b</varname> have:
|
|
|
|
</para>
|
|
|
|
<screen>
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[0] => 1
|
|
|
|
[1] => 8
|
|
|
|
[2] => 27
|
|
|
|
[3] => 64
|
|
|
|
[4] => 125
|
|
|
|
)
|
2008-08-30 14:23:24 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
2010-10-15 15:09:39 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2008-08-30 14:23:24 +00:00
|
|
|
<example>
|
2009-07-03 18:21:37 +00:00
|
|
|
<title><function>array_map</function> using a lambda function (as of PHP 5.3.0)</title>
|
2008-08-30 14:23:24 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2009-07-03 18:21:37 +00:00
|
|
|
$func = function($value) {
|
|
|
|
return $value * 2;
|
|
|
|
};
|
2008-08-30 14:23:24 +00:00
|
|
|
|
|
|
|
print_r(array_map($func, range(1, 5)));
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[0] => 2
|
|
|
|
[1] => 4
|
|
|
|
[2] => 6
|
|
|
|
[3] => 8
|
|
|
|
[4] => 10
|
|
|
|
)
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_map</function> - using more arrays</title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2002-11-11 00:36:16 +00:00
|
|
|
<?php
|
2006-10-31 11:24:02 +00:00
|
|
|
function show_Spanish($n, $m)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2019-05-15 11:39:08 +00:00
|
|
|
return "The number {$n} is called {$m} in Spanish";
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2006-10-31 11:24:02 +00:00
|
|
|
function map_Spanish($n, $m)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2019-05-15 11:39:08 +00:00
|
|
|
return [$n => $m];
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 11:39:08 +00:00
|
|
|
$a = [1, 2, 3, 4, 5];
|
|
|
|
$b = ['uno', 'dos', 'tres', 'cuatro', 'cinco'];
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2019-05-15 11:39:08 +00:00
|
|
|
$c = array_map('show_Spanish', $a, $b);
|
2002-04-15 00:12:54 +00:00
|
|
|
print_r($c);
|
|
|
|
|
2019-05-15 11:39:08 +00:00
|
|
|
$d = array_map('map_Spanish', $a , $b);
|
2002-04-15 00:12:54 +00:00
|
|
|
print_r($d);
|
2002-11-11 00:36:16 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
|
|
|
// printout of $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
|
|
|
|
)
|
|
|
|
|
|
|
|
// printout of $d
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[0] => Array
|
|
|
|
(
|
|
|
|
[1] => uno
|
|
|
|
)
|
|
|
|
|
|
|
|
[1] => Array
|
|
|
|
(
|
|
|
|
[2] => dos
|
|
|
|
)
|
|
|
|
|
|
|
|
[2] => Array
|
|
|
|
(
|
|
|
|
[3] => tres
|
|
|
|
)
|
|
|
|
|
|
|
|
[3] => Array
|
|
|
|
(
|
|
|
|
[4] => cuatro
|
|
|
|
)
|
|
|
|
|
|
|
|
[4] => Array
|
|
|
|
(
|
|
|
|
[5] => cinco
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Usually when using two or more arrays, they should be of equal length
|
|
|
|
because the callback function is applied in parallel to the corresponding
|
|
|
|
elements.
|
2013-09-16 08:27:44 +00:00
|
|
|
If the arrays are of unequal length, shorter ones will be extended with empty
|
|
|
|
elements to match the length of the longest.
|
2006-10-31 11:24:02 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
An interesting use of this function is to construct an array of arrays,
|
|
|
|
which can be easily performed by using &null;
|
|
|
|
as the name of the callback function
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
2019-05-21 19:56:39 +00:00
|
|
|
<title>Performing a zip operation of arrays</title>
|
2006-10-31 11:24:02 +00:00
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2002-11-11 00:36:16 +00:00
|
|
|
<?php
|
2019-05-15 11:39:08 +00:00
|
|
|
$a = [1, 2, 3, 4, 5];
|
|
|
|
$b = ['one', 'two', 'three', 'four', 'five'];
|
|
|
|
$c = ['uno', 'dos', 'tres', 'cuatro', 'cinco'];
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
$d = array_map(null, $a, $b, $c);
|
|
|
|
print_r($d);
|
2002-11-11 00:36:16 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
2002-04-15 00:12:54 +00:00
|
|
|
<![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
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
2008-01-20 14:56:59 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
2019-05-21 16:15:12 +00:00
|
|
|
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>
|
|
|
|
&null; <parameter>callback</parameter> with only
|
2020-11-02 11:17:26 +00:00
|
|
|
<parameter>array</parameter>
|
2019-05-21 16:15:12 +00:00
|
|
|
</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>
|
2008-01-20 14:56:59 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_map</function> - with string keys</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2019-05-15 11:39:08 +00:00
|
|
|
$arr = ['stringkey' => 'value'];
|
2008-01-20 14:56:59 +00:00
|
|
|
function cb1($a) {
|
2019-05-15 11:39:08 +00:00
|
|
|
return [$a];
|
2008-01-20 14:56:59 +00:00
|
|
|
}
|
|
|
|
function cb2($a, $b) {
|
2019-05-15 11:39:08 +00:00
|
|
|
return [$a, $b];
|
2008-01-20 14:56:59 +00:00
|
|
|
}
|
2019-05-15 11:39:08 +00:00
|
|
|
var_dump(array_map('cb1', $arr));
|
|
|
|
var_dump(array_map('cb2', $arr, $arr));
|
2008-01-20 14:56:59 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
2007-06-18 22:49:15 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
2006-10-31 11:24:02 +00:00
|
|
|
<para>
|
2007-06-18 22:49:15 +00:00
|
|
|
<simplelist>
|
|
|
|
<member><function>array_filter</function></member>
|
|
|
|
<member><function>array_reduce</function></member>
|
|
|
|
<member><function>array_walk</function></member>
|
|
|
|
</simplelist>
|
2006-10-31 11:24:02 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
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
|
|
|
|
-->
|