1
0
mirror of https://github.com/php/doc-fr.git synced 2026-03-25 15:42:16 +01:00
Files
archived-doc-fr/reference/array/functions/array-map.xml
Jean-Sébastien Goupil dcbd76b0cc sync with EN
git-svn-id: https://svn.php.net/repository/phpdoc/fr/trunk@222717 c90b9560-bf6c-de11-be94-00142212c4b1
2006-11-03 12:03:58 +00:00

232 lines
5.0 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.21 $ -->
<!-- EN-Revision: 1.17 Maintainer: dams Status: ready -->
<!-- Reviewed: no -->
<refentry id="function.array-map">
<refnamediv>
<refname>array_map</refname>
<refpurpose>Applique une fonction sur les éléments d'un tableau</refpurpose>
</refnamediv>
<refsect1>
&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> retourne un tableau
contenant tous les éléments du tableau <parameter>arr1</parameter>,
après leur avoir appliqué la fonction <parameter>callback</parameter>.
Le nombre de paramètres de la fonction <parameter>callback</parameter>
doit être égal au nombre de tableaux passés dans la fonction
<function>array_map</function>, dans les arguments supplémentaires
<parameter>...</parameter>.
</para>
<para>
<example>
<title>Exemple avec <function>array_map</function></title>
<programlisting role="php">
<![CDATA[
<?php
function cube($n) {
return $n*$n*$n;
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>array_map</function> : utilisation de plusieurs tableaux</title>
<programlisting role="php">
<![CDATA[
<?php
function parle_espagnol($n, $m) {
return "Le nombre $n se dit $m en espagnol";
}
function map_espagnol($n, $m) {
return array($n => $m);
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("parle_espagnol", $a, $b);
print_r($c);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
// Contenu de $c
Array
(
[0] => Le nombre 1 se dit uno en espagnol
[1] => Le nombre 2 se dit dos en espagnol
[2] => Le nombre 3 se dit tres en espagnol
[3] => Le nombre 4 se dit cuatro en espagnol
[4] => Le nombre 5 se dit cinco en espagnol
)
// Contenu 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>
Généralement, lors de l'utilisation de plusieurs tableaux, ils doivent
être d'égale longueur, car la fonction de callback est appliquée de manière
similaire à tous les tableaux. Si les tableaux sont de tailles inégales,
les plus petits seront complétés avec des éléments vides.
</para>
<para>
Une utilisation intéressante de cette fonction est la construction
de tableaux de tableaux, facilement réalisée en passant la valeur
&null; comme nom de fonction de callback.
</para>
<para>
<example>
<title>Création d'un tableau de tableaux</title>
<programlisting role="php">
<![CDATA[
<?php
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array("un", "deux","trois","quatre","cinq");
$d = array_map(null, $a, $b, $c, $d);
print_r($d);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => 1
[1] => one
[2] => uno
[3] => un
)
[1] => Array
(
[0] => 2
[1] => two
[2] => dos
[3] => deux
)
[2] => Array
(
[0] => 3
[1] => three
[2] => tres
[3] => trois
)
[3] => Array
(
[0] => 4
[1] => four
[2] => cuatro
[3] => quatre
)
[4] => Array
(
[0] => 5
[1] => five
[2] => cinco
[3] => cinq
)
)
]]>
</screen>
</example>
</para>
<para>
Voir aussi
<function>array_filter</function>,
<function>array_reduce</function>,
<function>array_walk</function>,
<function>create_function</function> et
&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
-->