1
0
mirror of https://github.com/php/doc-it.git synced 2026-03-24 15:42:46 +01:00
Files
archived-doc-it/reference/array/functions/array-map.xml
Richard Quadling 69b5799cfc Reencode XML to UTF-8
git-svn-id: https://svn.php.net/repository/phpdoc/it/trunk@314534 c90b9560-bf6c-de11-be94-00142212c4b1
2011-08-08 15:35:54 +00:00

238 lines
5.1 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- splitted from ./it/functions/array.xml, last change in rev 1.1 -->
<!-- last change to 'array-map' in en/ tree in rev 1.62 -->
<!-- EN-Revision: n/a Maintainer: cucinato Status: ready -->
<!-- OLD-Revision: 1.173/EN.1.62 -->
<refentry xml:id="function.array-map" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_map</refname>
<refpurpose>
Applica la funzione callback a tutti gli elementi dell'array dato
</refpurpose>
</refnamediv>
<refsect1>
<title>Descrizione</title>
<methodsynopsis>
<type>array</type><methodname>array_map</methodname>
<methodparam><type>callback</type><parameter>funzione</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> restituisce un array contenente tutti
gli elementi di <parameter>arr1</parameter> dopo che è stata loro applicata
la <parameter>funzione</parameter> callback.
Il numero di parametri che la <parameter>funzione</parameter>
callback accetta deve
corrispondere al numero di array
passati alla funzione <function>array_map</function>
</para>
<para>
<example>
<title>Esempio di <function>array_map</function></title>
<programlisting role="php">
<![CDATA[
<?php
function cubo($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cubo", $a);
print_r($b);
?>
]]>
</programlisting>
<para>
In questo modo <varname>$b</varname> sarà:
</para>
<screen>
<![CDATA[
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>array_map</function> - usare più array</title>
<programlisting role="php">
<![CDATA[
<?php
function mostra_Spagnolo($n, $m)
{
return("Il numero $n si dice $m in Spagnolo");
}
function mappa_Spagnolo($n, $m)
{
return(array($n => $m));
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("mostra_Spagnolo", $a, $b);
print_r($c);
$d = array_map("mappa_Spagnolo", $a, $b);
print_r($d);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
//stampa di $c
Array
(
[0] => Il numero 1 si dice uno in Spagnolo
[1] => Il numero 2 si dice dos in Spagnolo
[2] => Il numero 3 si dice tres in Spagnolo
[3] => Il numero 4 si dice cuatro in Spagnolo
[4] => Il numero 5 si dice cinco in Spagnolo
)
// stampa di $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>
Generalmente, quando si usano due o più array, questi devono avere eguale lunghezza
in quanto la funzione callback viene applicata in parallelo agli elementi
corrispondenti.
Se gli array sono di lunghezza diversa, il più corto verrà esteso
con elementi vuoti.
</para>
<para>
Un uso interessante di questa funzione è quello di costruire un array di array,
cosa che può essere facilmente ottenuta usando &null;
come nome della funzione callback
</para>
<para>
<example>
<title>Creare un array di array</title>
<programlisting role="php">
<![CDATA[
<?php
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "due", "tre", "quattro", "cinque");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
print_r($d);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => 1
[1] => uno
[2] => uno
)
[1] => Array
(
[0] => 2
[1] => due
[2] => dos
)
[2] => Array
(
[0] => 3
[1] => tre
[2] => tres
)
[3] => Array
(
[0] => 4
[1] => quattro
[2] => cuatro
)
[4] => Array
(
[0] => 5
[1] => cinque
[2] => cinco
)
)
]]>
</screen>
</example>
</para>
<para>
Vedere anche <function>array_filter</function>,
<function>array_reduce</function>,
<function>array_walk</function> e
&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:"~/.phpdoc/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
-->