mirror of
https://github.com/php/doc-de.git
synced 2026-03-24 07:12:15 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/de/trunk@200309 c90b9560-bf6c-de11-be94-00142212c4b1
238 lines
5.0 KiB
XML
238 lines
5.0 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- $Revision: 1.8 $ -->
|
|
<!-- EN-Revision: 1.15 Maintainer: simp Status: ready -->
|
|
<!-- CREDITS: tom -->
|
|
<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>callback</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> gibt ein Array mit allen Elementen von
|
|
<parameter>arr1</parameter> zurück, nachdem die Callback-Funktion
|
|
<parameter>callback</parameter> auf jedes einzelne Element angewandt
|
|
wurde. Die Anzahl Parameter, welche die Callback-Funktion
|
|
<parameter>callback</parameter> 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[
|
|
<?php
|
|
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>:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => 1
|
|
[1] => 8
|
|
[2] => 27
|
|
[3] => 64
|
|
[4] => 125
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
<function>array_map</function> - Verwendung mehrerer Arrays
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
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>
|
|
&example.outputs;
|
|
<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>
|
|
</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[
|
|
<?php
|
|
$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.outputs;
|
|
<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>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
Siehe auch <function>array_filter</function>,
|
|
<function>array_reduce</function>,
|
|
<function>array_walk</function>, und
|
|
&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:"../../../../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
|
|
-->
|