function.array-map.html 21 KB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Wendet eine Callback-Funktion auf die Elemente von Arrays an</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-keys.html">array_keys</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-merge-recursive.html">array_merge_recursive</a></div>
 <div class="up"><a href="ref.array.html">Array Funktionen</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-map" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_map</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5)</p><p class="refpurpose"><span class="refname">array_map</span> &mdash; <span class="dc-title">Wendet eine Callback-Funktion auf die Elemente von Arrays an</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.array-map-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>array_map</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <code class="parameter">$callback</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$arr1</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_map()</strong></span> gibt ein Array mit allen Elementen von
   <code class="parameter">arr1</code> zurück, nachdem die Callback-Funktion
   <code class="parameter">callback</code> auf jedes einzelne Element angewandt
   wurde. Die Anzahl Parameter, welche die Callback-Funktion
   <code class="parameter">callback</code> akzeptiert, sollte der Anzahl der an
   <span class="function"><strong>array_map()</strong></span> übergebenen Arrays entsprechen.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.array-map-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       Callback-Funktion, die für jedes Element in jedem Array
       aufgerufen werden soll.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">arr1</code></dt>

     <dd>

      <p class="para">
       Ein Array, auf das die <code class="parameter">callback</code>-Funktion
       angewendet werden soll.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       Variable Liste von Array-Argumenten, auf die die
       <code class="parameter">callback</code>-Funktion angewendet werden soll.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-map-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Gibt ein Array zurück, dass alle Elemente von
   <code class="parameter">arr1</code> enthält, nachdem die
   <code class="parameter">callback</code>-Funktion auf jedes Element
   angewendet wurde.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-map-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-4916">
    <p><strong>Beispiel #1 <span class="function"><strong>array_map()</strong></span>-Beispiel</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">cube</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cube"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Dies speichert in <var class="varname"><var class="varname">$b</var></var>:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 1
    [1] =&gt; 8
    [2] =&gt; 27
    [3] =&gt; 64
    [4] =&gt; 125
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4917">
    <p><strong>Beispiel #2 <span class="function"><strong>array_map()</strong></span> mit einer Lambda-Funktion
    (seit PHP 5.3.0)</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;Seit&nbsp;PHP&nbsp;5.3.0&nbsp;*/<br /><br /></span><span style="color: #0000BB">$func&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;&nbsp;};<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 2
    [1] =&gt; 4
    [2] =&gt; 6
    [3] =&gt; 8
    [4] =&gt; 10
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4918">
    <p><strong>Beispiel #3 
     <span class="function"><strong>array_map()</strong></span> - Verwendung mehrerer Arrays
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">show_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</span><span style="color: #DD0000">"Die&nbsp;Zahl&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #DD0000">&nbsp;heißt&nbsp;auf&nbsp;Spanisch&nbsp;&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">map_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(array(</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"uno"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"dos"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"tres"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cuatro"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cinco"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"show_Spanish"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"map_Spanish"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende
Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
// Ausgabe von $c
Array
(
    [0] =&gt; Die Zahl 1 heißt auf Spanisch uno
    [1] =&gt; Die Zahl 2 heißt auf Spanisch dos
    [2] =&gt; Die Zahl 3 heißt auf Spanisch tres
    [3] =&gt; Die Zahl 4 heißt auf Spanisch cuatro
    [4] =&gt; Die Zahl 5 heißt auf Spanisch cinco
)

// Ausgabe von $d
Array
(
    [0] =&gt; Array
        (
            [1] =&gt; uno
        )

    [1] =&gt; Array
        (
            [2] =&gt; dos
        )

    [2] =&gt; Array
        (
            [3] =&gt; tres
        )

    [3] =&gt; Array
        (
            [4] =&gt; cuatro
        )

    [4] =&gt; Array
        (
            [5] =&gt; cinco
        )

)
</pre></div>
    </div>
   </div>
  </p>
  <p class="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.
  </p>
  <p class="para">
   Eine interessante Anwendung dieser Funktion ist die Konstruktion
   eines Arrays bestehend aus Arrays, was mit <strong><code>NULL</code></strong> als Name der
   Callback-Funktion leicht realisiert werden kann.
  </p>
  <p class="para">
   <div class="example" id="example-4919">
    <p><strong>Beispiel #4 Erstellen eines Arrays mit Arrays</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"eins"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"zwei"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"drei"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"vier"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"fünf"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"uno"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"dos"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"tres"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cuatro"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cinco"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende
Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; eins
            [2] =&gt; uno
        )

    [1] =&gt; Array
        (
            [0] =&gt; 2
            [1] =&gt; zwei
            [2] =&gt; dos
        )

    [2] =&gt; Array
        (
            [0] =&gt; 3
            [1] =&gt; drei
            [2] =&gt; tres
        )

    [3] =&gt; Array
        (
            [0] =&gt; 4
            [1] =&gt; vier
            [2] =&gt; cuatro
        )

    [4] =&gt; Array
        (
            [0] =&gt; 5
            [1] =&gt; fünf
            [2] =&gt; cinco
        )

)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   Wenn das Array-Argument String-Schlüssel enthält, wird das
   zurückgegebene Array String-Schlüssel enthalten sofern genau ein
   Array übergeben wurde. Sollten mehrere Arrays übergeben werden,
   so hat das zurückgegebene Array immer Integer-Schlüssel.
  </p>
  <p class="para">
   <div class="example" id="example-4920">
    <p><strong>Beispiel #5 <span class="function"><strong>array_map()</strong></span> - mit String-Schlüsseln</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"stringkey"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"value"</span><span style="color: #007700">);<br />function&nbsp;</span><span style="color: #0000BB">cb1</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array&nbsp;(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />}<br />function&nbsp;</span><span style="color: #0000BB">cb2</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array&nbsp;(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cb1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cb2"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende
Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [&quot;stringkey&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(5) &quot;value&quot;
  }
}
array(1) {
  [0]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;value&quot;
    [1]=&gt;
    string(5) &quot;value&quot;
  }
}
array(1) {
  [&quot;stringkey&quot;]=&gt;
  string(5) &quot;value&quot;
}
array(1) {
  [0]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;value&quot;
    [1]=&gt;
    string(5) &quot;value&quot;
  }
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-map-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-filter.html" class="function" rel="rdfs-seeAlso">array_filter()</a> - Filtert Elemente eines Arrays mittels einer Callback-Funktion</span></li>
    <li class="member"><span class="function"><a href="function.array-reduce.html" class="function" rel="rdfs-seeAlso">array_reduce()</a> - Iterative Reduktion eines Arrays zu einem Wert mittels einer
     Callbackfunktion</span></li>
    <li class="member"><span class="function"><a href="function.array-walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - Wendet eine Benutzerfunktion auf jedem Element eines Arrays an</span></li>
    <li class="member"><span class="function"><a href="function.create-function.html" class="function" rel="rdfs-seeAlso">create_function()</a> - Erzeugen einer anonymen / tempor&auml;ren (Lambda-Stil) Funktion</span></li>
    <li class="member">Informationen über den <a href="language.pseudo-types.html#language.types.callback" class="link">Callback</a>-Typ</li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-keys.html">array_keys</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-merge-recursive.html">array_merge_recursive</a></div>
 <div class="up"><a href="ref.array.html">Array Funktionen</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>