Files
doc-fr/reference/array/functions/array-map.xml
Hartmut Holzgraefe b8daeaab85 banana-split
git-svn-id: https://svn.php.net/repository/phpdoc/fr/trunk@78204 c90b9560-bf6c-de11-be94-00142212c4b1
2002-04-15 00:40:38 +00:00

193 lines
5.0 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">
&lt;?php
function cube($n) {
return $n*$n*$n;
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
?&gt;
</programlisting>
</example>
</para>
<para>
Avec cet exemple, la variable <varname>$b</varname> contiendra
<literal>array (1, 8, 27, 64, 125);</literal>.
</para>
<para>
<example>
<title><function>array_map</function> - utilisation de plusieurs tableaux</title>
<programlisting role="php">
&lt;?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);
// Affichera :
// 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
// )
$d = array_map("map_espagnol", $a , $b);
print_r($d);
// Affichera :
// Array
// (
// [0] => Array
// (
// [1] => uno
// )
//
// [1] => Array
// (
// [2] => dos
// )
//
// [2] => Array
// (
// [3] => tres
// )
//
// [3] => Array
// (
// [4] => cuatro
// )
//
// [4] => Array
// (
// [5] => cinco
// )
//
// )
?&gt;
</programlisting>
</example>
</para>
<para>
G&eacute;n&eacute;ralement, lorsque vous utilisez plusieurs tableaux, ils doivent
&ecirc;tre de m&ecirc;me longueur, car la fonction de callback est appliqu&eacute; &agrave;
un &eacute;l&eacute;ment de chaque tableau. Si les tableaux sont de taille in&eacute;gale,
les plus petits seront compl&eacute;t&eacute;s avec des &eacute;l&eacute;ments vides.
</para>
<para>
Une utilisation interessante de cette fonction est de construire des
tableaux de tableaux, gr&acirc;ce &agrave; la fonction de callback &null;.
</para>
<para>
<example>
<title><function>array_map</function> - cr&eacute;ation d'un tableau de tableaux</title>
<programlisting role="php">
&lt;?php
$a = array(1, 2, 3, 4, 5);
$b = array("un", "deux", "trois", "quatre", "cinq");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
print_r($d);
// affichera :
// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => un
// [2] => uno
// )
//
// [1] => Array
// (
// [0] => 2
// [1] => deux
// [2] => dos
// )
//
// [2] => Array
// (
// [0] => 3
// [1] => trois
// [2] => tres
// )
//
// [3] => Array
// (
// [0] => 4
// [1] => quatre
// [2] => cuatro
// )
//
// [4] => Array
// (
// [0] => 5
// [1] => cinq
// [2] => cinco
// )
//
// )
?&gt;
</programlisting>
</example>
</para>
<para>
Voir aussi
<function>array_filter</function> et
<function>array_reduce</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
-->