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
Damien Seguy 7eb4275089 typo brooming
git-svn-id: https://svn.php.net/repository/phpdoc/fr/trunk@123872 c90b9560-bf6c-de11-be94-00142212c4b1
2003-04-18 14:06:29 +00:00

233 lines
5.3 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- splitted from ./fr/functions/array.xml, last change in rev 1.30 -->
<!-- last change to 'array-map' in en/ tree in rev 1.62 -->
<refentry id="function.array-map">
<refnamediv>
<refname>array_map</refname>
<refpurpose>Applique sur fonction sur des tableaux</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_map</methodname>
<methodparam><type>mixed</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 &eacute;l&eacute;ments du tableau <parameter>arr1</parameter>,
apr&egrave;s leur avoir appliqu&eacute; la fonction <parameter>callback</parameter>.
Le nombre de param&egrave;tres de la fonction <parameter>callback</parameter>
doit &ecirc;tre &eacute;gal au nombre de tableaux pass&eacute;s dans la fonction
<function>array_map</function>.
</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);
?>
]]>
</programlisting>
<para>
Cela conduit &agrave; ce que la variable <varname>$b</varname> contienne :
<screen>
<![CDATA[
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
]]>
</screen>
</para>
</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>
<para>
Cela va afficher :
<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>
</para>
</example>
</para>
<para>
G&eacute;n&eacute;ralement, lors de l'utilisation de plusieurs tableaux, ils doivent
&ecirc;tre d'&eacute;gale longueur, car la fonction de callback est appliqu&eacute; de mani&egrave;re
similaire &agrave; tous les tableaux. Si les tableaux sont de taille in&eacute;gales,
les plus petits seront compl&eacute;t&eacute;s avec des &eacute;l&eacute;ments vides.
</para>
<para>
Une utilisation int&eacute;ressante de cette fonction est la construction
de tableaux de tableaux, facilement r&eacute;alis&eacute; en passant la valeur
&null; comme nom de fonction de callback.
</para>
<para>
<example>
<title>Cr&eacute;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>
</para>
<para>
Le programme ci-dessus va afficher :
<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>
</para>
<para>
Voir aussi
<function>array_filter</function>,
<function>array_reduce</function> et
<function>array_walk</function>.
</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
-->