mirror of
https://github.com/php/doc-fr.git
synced 2026-03-27 00:22:21 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/fr/trunk@78204 c90b9560-bf6c-de11-be94-00142212c4b1
193 lines
5.0 KiB
XML
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 é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>.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Exemple avec <function>array_map</function></title>
|
|
<programlisting role="php">
|
|
<?php
|
|
function cube($n) {
|
|
return $n*$n*$n;
|
|
}
|
|
$a = array(1, 2, 3, 4, 5);
|
|
$b = array_map("cube", $a);
|
|
?>
|
|
</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">
|
|
<?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
|
|
// )
|
|
//
|
|
// )
|
|
?>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
Généralement, lorsque vous utilisez plusieurs tableaux, ils doivent
|
|
être de même longueur, car la fonction de callback est appliqué à
|
|
un élément de chaque tableau. Si les tableaux sont de taille inégale,
|
|
les plus petits seront complétés avec des éléments vides.
|
|
</para>
|
|
<para>
|
|
Une utilisation interessante de cette fonction est de construire des
|
|
tableaux de tableaux, grâce à la fonction de callback &null;.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>array_map</function> - création d'un tableau de tableaux</title>
|
|
<programlisting role="php">
|
|
<?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
|
|
// )
|
|
//
|
|
// )
|
|
?>
|
|
</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
|
|
-->
|