1
0
mirror of https://github.com/php/doc-de.git synced 2026-03-24 15:22:14 +01:00
Files
archived-doc-de/reference/array/functions/array-map.xml
Mark Kronsbein c5867d8e23 Add Revision tags Pt. 2
git-svn-id: https://svn.php.net/repository/phpdoc/de/trunk@84245 c90b9560-bf6c-de11-be94-00142212c4b1
2002-05-31 17:47:11 +00:00

229 lines
4.9 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- EN-Revision: 1.4 Maintainer: tom Status: ready -->
<refentry id="function.array-map">
<refnamediv>
<refname>array_map</refname>
<refpurpose>
Wendet eine Callback-Funktion auf die Elemente von Arrays an
</refpurpose>
</refnamediv>
<refsect1>
<title>Beschreibung</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>arr2...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_map</function> gibt ein Array mit allen Elementen von
<parameter>arr1</parameter> zurück, nachdem die Callback-Funktion auf
jedes einzelne Element angewandt wurde. Die Anzahl Parameter, welche
Callback-Funktion akzeptiert, sollte der Anzahl der an
<function>array_map</function> übergebenen Arrays entsprechen.
</para>
<para>
<example>
<title><function>array_map</function></title>
<programlisting role="php">
<![CDATA[
function cube($n) {
return $n*$n*$n;
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
]]>
</programlisting>
<para>
Dies speichert in <varname>$b</varname>:
<screen>
<![CDATA[
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
]]>
</screen>
</para>
</example>
</para>
<para>
<example>
<title>
<function>array_map</function> - Verwendung mehrerer Arrays
</title>
<programlisting role="php">
<![CDATA[
function show_Spanish($n, $m) {
return "Die Zahl $n heißt auf Spanisch $m";
}
function map_Spanish($n, $m) {
return array ($n => $m);
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("show_Spanish", $a, $b);
print_r($c);
$d = array_map("map_Spanish", $a , $b);
print_r($d);
]]>
</programlisting>
<para>
Dies resultiert in:
<screen>
<![CDATA[
// Ausgabe von $c
Array
(
[0] => Die Zahl 1 heißt auf Spanisch uno
[1] => Die Zahl 2 heißt auf Spanisch dos
[2] => Die Zahl 3 heißt auf Spanisch tres
[3] => Die Zahl 4 heißt auf Spanisch cuatro
[4] => Die Zahl 5 heißt auf Spanisch cinco
)
// Ausgabe von $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>
Bei Verwendung von zwei oder mehr Arrays sollten diese gewöhnlich
die gleiche Länge besitzen, da die Callback-Funktion parallel auf
die entsprechenden Elemente angewandt wird. Haben die Arrays
unterschiedliche Längen, wird das kürzeste um leere Elemente
erweitert.
</para>
<para>
Eine interessante Anwendung dieser Funktion ist die Konstruktion
eines Arrays bestehend aus Arrays, was mit &null; als Name der
Callback-Funktion leicht realisiert werden kann.
</para>
<para>
<example>
<title>Erstellen eines Arrays mit Arrays</title>
<programlisting role="php">
<![CDATA[
$a = array(1, 2, 3, 4, 5);
$b = array("eins", "zwei", "drei", "vier", "fünf");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
print_r($d);
]]>
</programlisting>
</example>
</para>
<para>
Würde folgendes ausgeben:
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => 1
[1] => eins
[2] => uno
)
[1] => Array
(
[0] => 2
[1] => zwei
[2] => dos
)
[2] => Array
(
[0] => 3
[1] => drei
[2] => tres
)
[3] => Array
(
[0] => 4
[1] => vier
[2] => cuatro
)
[4] => Array
(
[0] => 5
[1] => fünf
[2] => cinco
)
)
]]>
</screen>
</para>
<para>
Siehe auch <function>array_filter</function> und
<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
-->