<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'language.types.array.php',
    1 => 'Arrays',
    2 => 'Arrays',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Typen',
  ),
  'prev' => 
  array (
    0 => 'language.types.numeric-strings.php',
    1 => 'Numerische Zeichenketten',
  ),
  'next' => 
  array (
    0 => 'language.types.object.php',
    1 => 'Objekte',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'language/types/array.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.array" class="sect1">
 <h2 class="title">Arrays</h2>

 <p class="para">
  Eine Liste aller Array-Funktionen findet sich im Kapitel
  <a href="ref.array.php" class="link">Array-Funktionen</a> der Dokumentation.
 </p>

 <p class="para">
  Ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> ist in PHP in Wirklichkeit eine geordnete Map
  (Abbildung). Eine Map ist ein Typ, der <em>Werte</em> zu
  <em>Schlüsseln</em> zuordnet. Dieser Typ ist für einige
  verschiedene Nutzungsarten optimiert. Er kann als ein Array verwendet
  werden, als Liste (Vektor), Hash-Tabelle (eine Implementierung einer Map),
  Wörterbuch, Sammlung, Stack, Queue (Warteschlange) und wahrscheinlich noch
  als vieles anderes. Da ein Wert eines <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s ein weiteres
  <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> sein kann, sind auch Bäume und mehrdimensionale
  <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s möglich.
 </p>

 <p class="para">
  Die Erläuterung dieser Datenstrukturen liegt nicht im Rahmen dieses
  Handbuchs, aber wenigstens ein Beispiel für jede ist hier angegeben. Um
  weitere Informationen zu erhalten wird auf die immense Literatur zu diesem
  weiten Feld verwiesen.
 </p>

 <div class="sect2" id="language.types.array.syntax">
  <h3 class="title">Syntax</h3>

  <div class="sect3" id="language.types.array.syntax.array-func">
   <h4 class="title">Spezifizierung mittels <span class="function"><a href="function.array.php" class="function">array()</a></span></h4>

   <p class="para">
    Ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> kann durch das Sprachkonstrukt
    <span class="function"><a href="function.array.php" class="function">array()</a></span> erzeugt werden. Dies nimmt eine beliebige
    Anzahl kommaseparierter <code class="literal"><span class="replaceable">Schlüssel</span>
    =&gt; <span class="replaceable">Wert</span></code>-Paare als Parameter
    entgegen.
   </p>

   <pre class="synopsis">
array(
    <span class="optional"><span class="replaceable">Schlüssel</span>  =&gt; </span><span class="replaceable">Wert</span>,
    <span class="optional"><span class="replaceable">Schlüssel2</span> =&gt; </span><span class="replaceable">Wert2</span>,
    <span class="optional"><span class="replaceable">Schlüssel3</span> =&gt; </span><span class="replaceable">Wert3</span>,
    ...
)</pre>
   

   <p class="para">
    Das Komma nach dem letzten Array-Element ist optional und kann weggelassen
    werden. Dies wird üblicherweise in einzeiligen Arrays getan, d. h..
    <code class="literal">array(1, 2)</code> wird der Schreibweise <code class="literal">array(1, 2,
    )</code> vorgezogen. Andererseits wird bei mehrzeiligen Arrays das
    Komma am Ende üblicherweise genutzt, da dies ein einfaches Hinzufügen
    weiterer Elemente am Ende erlaubt.
   </p>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Es gibt eine Kurzform der Array-Syntax, die <code class="literal">array()</code>
     durch <code class="literal">[]</code> ersetzt.
    </p>
   </p></blockquote>

   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Ein einfaches Array</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array1 </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"bar" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />);<br /><br /></span><span style="color: #FF8000">// Verwendung der verkürzten Array-Syntax<br /></span><span style="color: #0000BB">$array2 </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"bar" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />];<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">, </span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Der <span class="replaceable">Schlüssel</span> kann entweder ein
    <span class="type"><a href="language.types.integer.php" class="link">Integer</a></span> oder ein <span class="type"><a href="language.types.string.php" class="link">String</a></span> sein. Der
    <span class="replaceable">Wert</span> kann beliebige Datentypen annehmen.
   </p>

   <p class="para" id="language.types.array.key-casts">
    Zusätzlich werden die folgenden Typkonvertierungen auf den
    <span class="replaceable">Schlüssel</span> angewendet:
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.string.php" class="type String">String</a></span>s, die einen gültigen dezimalen <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span>
       enthalten, werden zum entsprechenden <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span> umgewandelt, es
       sei denn, der Zahl ist ein <code class="literal">+</code>-Zeichen vorangestellt.
       Beispielsweise wird der Schlüssel <code class="literal">&quot;8&quot;</code> tatsächlich
       unter <code class="literal">8</code> gespeichert. Andererseits wird
       <code class="literal">&quot;08&quot;</code> nicht umgewandelt, da dies kein gültiger
       Ganzzahlwert ist.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.float.php" class="type Float">Float</a></span>s werden ebenfalls zu <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span> umgewandelt,
       was ein Abschneiden des Dezimalbruchs zur Folge hat. Beispielsweise
       wird der Schlüssel <code class="literal">8.7</code> als <code class="literal">8</code>
       gespeichert.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.boolean.php" class="type Bool">Bool</a></span>s werden ebenfalls zu <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span> umgewandelt,
       d. h. der Schlüssel <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> wird unter <code class="literal">1</code> gespeichert
       und der Schlüssel <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> unter <code class="literal">0</code>.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.null.php" class="type Null">Null</a></span> wird zu einem leeren String umgewandelt, d. h. der
       Schlüssel <code class="literal">null</code> wird tatsächlich unter
       <code class="literal">&quot;&quot;</code> gespeichert.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Werte vom Typ <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> oder <span class="type"><a href="language.types.object.php" class="link">Objekt</a></span>
       <em>können nicht</em> als Schlüssel verwendet werden. Ein
       Versuch, dies zu tun, wird die Warnung <code class="literal">Illegal offset
       type</code> zur Folge haben.
      </span>
     </li>
    </ul>
   </p>

   <p class="para">
    Wenn mehrere Elemente in einer Array-Deklaration den gleichen Schlüssel
    verwenden, dann wird nur der letzte verwendet und alle weiteren werden
    überschrieben.
   </p>

   <div class="example" id="example-2">
    <p><strong>Beispiel #2 Beispiel für Typumwandlung und Überschreibung</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br />    </span><span style="color: #0000BB">1    </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"a"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"1"  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"b"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">1.5  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"c"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">true </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"d"</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</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="annotation-interactive cdata"><pre>
array(1) {
  [1]=&gt;
  string(1) &quot;d&quot;
}
</pre></div>
    </div>
    <div class="example-contents"><p>
     Da alle Schlüssel im obigen Beispiel zu <code class="literal">1</code> umgewandelt
     werden, wird der Wert dieses Elements durch jeden angegebenen Wert
     überschrieben und nur der letzte zugewiesene Wert <code class="literal">&quot;d&quot;</code>
     bleibt übrig.
    </p></div>
   </div>

   <p class="para">
    Arrays können in PHP gleichzeitig Schlüssel der Typen <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span> und
    <span class="type"><a href="language.types.string.php" class="type String">String</a></span> enthalten, da PHP nicht zwischen indizierten und
    assoziativen Arrays unterscheidet.
   </p>

   <div class="example" id="example-3">
    <p><strong>Beispiel #3 Gemischte <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span>- und <span class="type"><a href="language.types.string.php" class="type String">String</a></span>-Schlüssel</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"bar" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">100   </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />    -</span><span style="color: #0000BB">100  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">100</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</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="annotation-interactive cdata"><pre>
array(4) {
  [&quot;foo&quot;]=&gt;
  string(3) &quot;bar&quot;
  [&quot;bar&quot;]=&gt;
  string(3) &quot;foo&quot;
  [100]=&gt;
  int(-100)
  [-100]=&gt;
  int(100)
}
</pre></div>
    </div>
   </div>

   <p class="para">
    Die Angabe des <span class="replaceable">Schlüssel</span>s ist optional. Ist
    keiner angegeben, so wird PHP den bisher größten angegebenen
    <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span>-Schlüssel erhöhen und das Ergebnis als Schlüssel
    verwenden.
   </p>

   <div class="example" id="example-4">
    <p><strong>Beispiel #4 Indizierte Arrays ohne Schlüssel</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">, </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</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="annotation-interactive cdata"><pre>
array(4) {
  [0]=&gt;
  string(3) &quot;foo&quot;
  [1]=&gt;
  string(3) &quot;bar&quot;
  [2]=&gt;
  string(5) &quot;hello&quot;
  [3]=&gt;
  string(5) &quot;world&quot;
}
</pre></div>
    </div>
   </div>

   <p class="para">
    Es ist auch möglich, den Schlüssel nur bei einigen Elementen anzugeben und
    bei anderen auszulassen:
   </p>

   <div class="example" id="example-5">
    <p><strong>Beispiel #5 Schlüssel nicht bei allen Elementen</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br />         </span><span style="color: #DD0000">"a"</span><span style="color: #007700">,<br />         </span><span style="color: #DD0000">"b"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">6 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"c"</span><span style="color: #007700">,<br />         </span><span style="color: #DD0000">"d"</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</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="annotation-interactive cdata"><pre>
array(4) {
  [0]=&gt;
  string(1) &quot;a&quot;
  [1]=&gt;
  string(1) &quot;b&quot;
  [6]=&gt;
  string(1) &quot;c&quot;
  [7]=&gt;
  string(1) &quot;d&quot;
}
</pre></div>
    </div>
    <div class="example-contents"><p>
     Hier ist zu erkennen, dass dem letzten Wert <code class="literal">&quot;d&quot;</code> der
     Schlüssel <code class="literal">7</code> zugewiesen wurde. Dies erfolgte, da der
     größte vorherige ganzzahlige Schlüssel <code class="literal">6</code> war.
    </p></div>
   </div>

   <div class="example" id="example-6">
    <p><strong>Beispiel #6 Beispiel für komplexe Typumwandlung und Überschreibung</strong></p>
    <div class="example-contents"><p>
     Dieses Beispiel enthält alle Variationen der Typumwandlung von Schlüsseln
     und des Überschreibens von Elementen.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br />    </span><span style="color: #0000BB">1    </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'a'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'1'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #FF8000">// der Wert "a" wird mit "b" überschrieben<br />    </span><span style="color: #0000BB">1.5  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'c'</span><span style="color: #007700">, </span><span style="color: #FF8000">// der Wert "b" wird mit "c" überschrieben<br />    </span><span style="color: #007700">-</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'d'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'01'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'e'</span><span style="color: #007700">, </span><span style="color: #FF8000">// da dies kein Integer-String ist, wird der Schlüssel<br />                  // für 1 NICHT überschrieben<br />    </span><span style="color: #DD0000">'1.5' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'f'</span><span style="color: #007700">, </span><span style="color: #FF8000">// da dies kein Integer-String ist, wird der Schlüssel<br />                  // für 1 NICHT überschrieben<br />    </span><span style="color: #0000BB">true </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'g'</span><span style="color: #007700">, </span><span style="color: #FF8000">// der Wert "c" wird mit "g" überschrieben<br />    </span><span style="color: #0000BB">false </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'h'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'i'</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">null </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'j'</span><span style="color: #007700">, </span><span style="color: #FF8000">// der Wert "i" wird mit "j" überschrieben<br />    </span><span style="color: #DD0000">'k'</span><span style="color: #007700">, </span><span style="color: #FF8000">// dem Wert "k" wird der Schlüssel 2 zugewiesen. Das liegt daran,<br />         // dass der größte ganzzahlige Schlüssel davor 1 war<br />    </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'l'</span><span style="color: #007700">, </span><span style="color: #FF8000">// der Wert "k" wird mit "l" überschrieben<br /></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</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="annotation-interactive cdata"><pre>
array(7) {
  [1]=&gt;
  string(1) &quot;g&quot;
  [-1]=&gt;
  string(1) &quot;d&quot;
  [&quot;01&quot;]=&gt;
  string(1) &quot;e&quot;
  [&quot;1.5&quot;]=&gt;
  string(1) &quot;f&quot;
  [0]=&gt;
  string(1) &quot;h&quot;
  [&quot;&quot;]=&gt;
  string(1) &quot;j&quot;
  [2]=&gt;
  string(1) &quot;l&quot;
}
</pre></div>
    </div>
   </div>

   <div class="example" id="example-7">
    <p><strong>Beispiel #7 Beispiel mit negativem Index</strong></p>
    <div class="example-contents"><p>
     Wenn ein negativer ganzzahliger Schlüssel <code class="literal">n</code> zugewiesen
     wird, stellt PHP sicher, dass als nächstes der Schlüssel
     <code class="literal">n+1</code> zugewiesen wird.
    </p></div>
    <div class="example-contents">
     <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= [];<br /><br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[-</span><span style="color: #0000BB">5</span><span style="color: #007700">] = </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">2</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</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="annotation-interactive cdata"><pre>
array(2) {
  [-5]=&gt;
  int(1)
  [-4]=&gt;
  int(2)
}
</pre></div>
    </div>

    <div class="warning"><strong class="warning">Warnung</strong>
     <p class="simpara">
      Vor PHP 8.3.0 würde die Zuweisung eines negativen ganzzahligen Schlüssels
      <code class="literal">n</code> dazu führen, dass als nächstes der Schlüssel
      <code class="literal">0</code> zugewiesen wird, sodass das obige Beispiel zu diesem
      Ergebnis führen würde:
     </p>
     <div class="informalexample">
      <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
array(2) {
  [-5]=&gt;
  int(1)
  [0]=&gt;
  int(2)
}
</pre></div>
      </div>
     </div>
    </div>
   </div>
  </div>

  <div class="sect3" id="language.types.array.syntax.accessing">
   <h4 class="title">Zugriff auf Elemente mit eckigen Klammern</h4>

   <p class="para">
    Auf Elemente eines Arrays kann durch Verwendung der Syntax
    <code class="literal">array[Schlüssel]</code> zugegriffen werden.
   </p>

   <div class="example" id="example-8">
    <p><strong>Beispiel #8 Zugriff auf Array-Elemente</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">42    </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">24</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"multi" </span><span style="color: #007700">=&gt; array(<br />         </span><span style="color: #DD0000">"dimensional" </span><span style="color: #007700">=&gt; array(<br />             </span><span style="color: #DD0000">"array" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"<br />         </span><span style="color: #007700">)<br />    )<br />);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">42</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"multi"</span><span style="color: #007700">][</span><span style="color: #DD0000">"dimensional"</span><span style="color: #007700">][</span><span style="color: #DD0000">"array"</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="annotation-interactive cdata"><pre>
string(3) &quot;bar&quot;
int(24)
string(3) &quot;foo&quot;
</pre></div>
    </div>
   </div>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Vor PHP 8.0.0 konnten sowohl eckige als auch geschweifte Klammern
     verwendet werden, um auf die Elemente eines Arrays zuzugreifen (z. B.
     werden im obigen Beispiel <code class="literal">$array[42]</code> und
     <code class="literal">array{42}</code> auf das gleiche Element zugreifen). Die
     Syntax für geschweifte Klammern ist seit PHP 7.4.0 veraltet und wird seit
     PHP 8.0.0 nicht mehr unterstützt.
    </p>
   </p></blockquote>

   <div class="example" id="example-9">
    <p><strong>Beispiel #9 Array-Dereferenzierung</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">getArray</span><span style="color: #007700">() {<br />    return array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$secondElement </span><span style="color: #007700">= </span><span style="color: #0000BB">getArray</span><span style="color: #007700">()[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$secondElement</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
      Der Versuch, auf einen nicht definierten Arrayschlüssel zuzugreifen,
      entspricht dem Zugriff auf jede andere undefinierte Variable: ein Fehler
      der Stufe <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> (der Stufe
      <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> vor PHP 8.0.0) wird ausgegeben und das
      Ergebnis ist <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Die Array-Dereferenzierung eines skalaren Werts, der kein
     <span class="type"><a href="language.types.string.php" class="type String">String</a></span> ist, ergibt <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. Vor PHP 7.4.0 verursachte dies
     keine Fehlermeldung. Seit PHP 7.4.0 wird ein Fehler der Stufe
     <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> ausgegeben; seit PHP 8.0.0 ein Fehler der
     Stufe <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
    </p>
   </p></blockquote>
  </div>

  <div class="sect3" id="language.types.array.syntax.modifying">
   <h4 class="title">Anlegen/Ändern mit eckigen Klammern</h4>

   <p class="para">
    Ein bestehendes <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> kann verändert werden, indem man
    explizit Werte darin setzt.
   </p>

   <p class="para">
    Dies wird getan, indem man dem <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> Werte zuweist und dabei
    den Schlüssel in eckigen Klammern angibt. Der Schlüssel kann dabei
    weggelassen werden, was ein leeres Klammernpaar (<code class="literal">[]</code>)
    ergibt.
   </p>

   <pre class="synopsis">
$arr[<span class="replaceable">Schlüssel</span>] = <span class="replaceable">Wert</span>;
$arr[] = <span class="replaceable">Wert</span>;
// <span class="replaceable">Schlüssel</span> kann ein <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span> oder <span class="type"><a href="language.types.string.php" class="type String">String</a></span> sein
// <span class="replaceable">Wert</span> kann einen beliebigen Typen haben</pre>

   <p class="para">
    Falls <var class="varname">$arr</var> noch nicht existiert oder auf <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> oder
    <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> gesetzt ist, wird es erzeugt; dies ist also eine alternative
    Methode, um ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> zu erzeugen. Dieses Vorgehen wird
    jedoch nicht empfohlen, da, falls <var class="varname">$arr</var> bereits einen
    Wert enthält (z. B. ein <span class="type"><a href="language.types.string.php" class="type String">String</a></span> aus einer Requestvariable),
    dieser Wert bestehen bleibt und <code class="literal">[]</code> tatsächlich für den
    <a href="language.types.string.php#language.types.string.substr" class="link">String-Zugriffs-Operator</a>
    stehen kann. Es ist immer besser, eine Variable durch direkte Zuweisung zu
    initialisieren.
   </p>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Seit PHP 7.1.0 löst die Anwendung des leeren Index-Operators auf eine
     Zeichenkette einen fatalen Fehler aus. Zuvor wurde die Zeichenkette
     stillschweigend in ein Array umgewandelt.
    </span>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Seit PHP 8.1.0 ist das Erstellen eines neuen Arrays aus <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>-Werten
     veraltet. Es ist weiterhin erlaubt, ein neues Array aus <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>- und
     undefinierten Werten zu erstellen.
    </span>
   </p></blockquote>

   <p class="para">
    Um einen bestimmten Wert zu ändern, kann man dem Element anhand seines
    Schlüssels einen Wert zuweisen. Um ein Schlüssel-Wert-Paar zu entfernen,
    kann man die Funktion <span class="function"><a href="function.unset.php" class="function">unset()</a></span> darauf anwenden.
   </p>

   <div class="example" id="example-10">
    <p><strong>Beispiel #10 Verwenden von eckigen Klammern bei Arrays</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #0000BB">5 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">12 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[] = </span><span style="color: #0000BB">56</span><span style="color: #007700">;    </span><span style="color: #FF8000">// Dies ist dasselbe wie $arr[13] = 56;<br />                // an dieser Stelle im Programmablauf<br /><br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"x"</span><span style="color: #007700">] = </span><span style="color: #0000BB">42</span><span style="color: #007700">; </span><span style="color: #FF8000">// Dies fügt ein neues Element zum Array<br />                // mit dem Schlüssel "x" hinzu<br /><br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]); </span><span style="color: #FF8000">// Dies entfernt das Element aus dem Array<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);<br /><br />unset(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);    </span><span style="color: #FF8000">// Dies löscht das gesamte Array<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Wie vorher bereits erwähnt, wird, wenn kein Schlüssel angegeben ist, das
     Maximum der bestehenden Schlüssel vom Typ <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span> verwendet und
     der neue Schlüssel wird das Maximum plus 1 sein (aber mindestens 0). Wenn
     noch kein <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span>-Schlüssel existiert wird der Schlüssel
     <code class="literal">0</code> (Null) verwendet.
    </p>

    <p class="para">
     Zu beachten ist, dass der Maximalwert der Integer-Schlüssel dafür
     <em>aktuell nicht im <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> existieren muss</em>.
     Er muss nur zu irgendeinem Zeitpunkt im <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> existiert
     haben, seitdem das <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> zuletzt neu indiziert wurde. Das
     folgende Beispiel erläutert dies:
    </p>

    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Create a simple array.<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Nun wird jeder Wert gelöscht, aber das Array selbst bleibt bestehen<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$array </span><span style="color: #007700">as </span><span style="color: #0000BB">$i </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br />    unset(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Ein Element wird hinzugefügt<br />// (Beachten Sie, dass der neue Schlüssel 5 ist statt 0)<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">6</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Neue Indizierung<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= </span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">7</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     <p class="para">Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p>
     <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
    [0] =&gt; 1
    [1] =&gt; 2
    [2] =&gt; 3
    [3] =&gt; 4
    [4] =&gt; 5
)
Array
(
)
Array
(
    [5] =&gt; 6
)
Array
(
    [0] =&gt; 6
    [1] =&gt; 7
)
</pre></div>
     </div>
    </div>

   </p></blockquote>

  </div>

  <div class="sect3" id="language.types.array.syntax.destructuring">
   <h4 class="title">Destrukturierung (Zerlegung) von Arrays</h4>

   <p class="para">
    Arrays können mit den Sprachkonstrukten <code class="literal">[]</code> (seit
    PHP 7.1.0) oder <span class="function"><a href="function.list.php" class="function">list()</a></span> destrukuriert werden. Diese
    Konstrukte können verwendet werden, um ein Array in einzelne Variablen zu
    zerlegen.
   </p>

   <div class="example" id="example-11">
    <p><strong>Beispiel #11 Destrukturieren eines Arrays</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br />[</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #0000BB">$bar</span><span style="color: #007700">, </span><span style="color: #0000BB">$baz</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt "foo" aus<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$bar</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt "bar" aus<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$baz</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt "baz" aus<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Die Destrukturierung von Arrays kann in <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> verwendet werden, um
    ein mehrdimensionales Array zu zerlegen, während darüber iteriert wird.
   </p>

   <div class="example" id="example-12">
    <p><strong>Beispiel #12 Destrukturieren eines Arrays in foreach</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [<br />    [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'John'</span><span style="color: #007700">],<br />    [</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">],<br />];<br /><br />foreach (</span><span style="color: #0000BB">$source_array </span><span style="color: #007700">as [</span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">]) {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$id</span><span style="color: #007700">}</span><span style="color: #DD0000">: '</span><span style="color: #007700">{</span><span style="color: #0000BB">$name</span><span style="color: #007700">}</span><span style="color: #DD0000">'\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Array-Elemente werden ignoriert, wenn die Variable nicht angegeben wird.
    Die Destrukturierung von Arrays beginnt immer beim Index
    <code class="literal">0</code>.
   </p>

   <div class="example" id="example-13">
    <p><strong>Beispiel #13 Ignoren von Elementen</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Zuweisen des Elements bei Index 2 an die Variable $baz<br /></span><span style="color: #007700">[, , </span><span style="color: #0000BB">$baz</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$baz</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt "baz" aus<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Seit PHP 7.1.0 können auch assoziative Arrays destrukturiert werden. Da
    der Index explizit angegeben werden kann, ermöglicht dies auch in
    numerisch indizierten Arrays eine einfachere Auswahl des richtigen
    Elements.
   </p>

   <div class="example" id="example-14">
    <p><strong>Beispiel #14 Destrukturieren von assoziativen Arrays</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Zuweisen des Elements bei Index 'baz' an die Variable $three<br /></span><span style="color: #007700">[</span><span style="color: #DD0000">'baz' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$three</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$three</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// gibt 3 aus<br /><br /></span><span style="color: #0000BB">$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Zuweisen des Elements bei Index 2 an die Variable $baz<br /></span><span style="color: #007700">[</span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$baz</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$baz</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt "baz" aus<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Array-Destrukturierung kann verwendet werden, um auf einfache Weise zwei
    Variablen zu vertauschen.
   </p>

   <div class="example" id="example-15">
    <p><strong>Beispiel #15 Vertauschen von zwei Variablen</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />[</span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">] = [</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">];<br /><br />echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt 2 aus<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// gibt 1 aus<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Der Spread-Operator (<code class="literal">...</code>) wird bei Zuweisungen
     nicht unterstützt.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Wird versucht, auf einen nicht definierten Array-Schlüssel zuzugreifen,
     so ist dies dasselbe wie der Zugriff auf eine andere undefinierte
     Variable: Eine Fehlermeldung der Stufe <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
     (der Stufe <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> vor PHP 8.0.0) wird ausgegeben
     und das Ergebnis ist <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Bei der Destrukturierung eines skalaren Werts wird allen Variablen <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>
     zugewiesen.
    </p>
   </p></blockquote>
  </div>

 </div>

 <div class="sect2" id="language.types.array.useful-funcs">
  <h3 class="title">Nützliche Funktionen</h3>

  <p class="para">
   Es gibt einige nützliche Funktionen für die Arbeit mit Arrays, die im
   Abschnitt <a href="ref.array.php" class="link">Array-Funktionen</a> nachgeschlagen
   werden können.
  </p>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Die Funktion <span class="function"><a href="function.unset.php" class="function">unset()</a></span> erlaubt es, Schlüssel aus einem
    <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> zu entfernen. Zu beachten ist dabei, dass das Array
    <em>nicht</em> neu indiziert wird. Falls ein echtes Verhalten
    von &quot;Entfernen und Verschieben&quot; gewünscht ist, dann kann das
    <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> mittels <span class="function"><a href="function.array-values.php" class="function">array_values()</a></span> neu indiziert
    werden.
   </p>

   <div class="example" id="example-16">
    <p><strong>Beispiel #16 Entfernen von Zwischenelementen</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'one'</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'two'</span><span style="color: #007700">, </span><span style="color: #0000BB">3 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'three'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* dies wird ein Array erzeugen, das wie folgt definiert worden wäre<br />   $a = array(1 =&gt; 'one', 3 =&gt; 'three');<br />   und NICHT so<br />   $a = array(1 =&gt; 'one', 2 =&gt;'three');<br />*/<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Nun ist $b array(0 =&gt; 'one', 1 =&gt;'three')<br /></span><span style="color: #0000BB">var_dump</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>
  </p></blockquote>

  <p class="para">
   Die Kontrollstruktur <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> existiert speziell für <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s.
   Sie bietet eine einfache Möglichkeit, ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> zu
   durchlaufen.
  </p>
 </div>

 <div class="sect2" id="language.types.array.donts">
  <h3 class="title">Dos und Dont&#039;s für Arrays (was man tun sollte und was nicht)</h3>

  <div class="sect3" id="language.types.array.foo-bar">
   <h4 class="title">Wieso ist <code class="literal">$foo[bar]</code> falsch?</h4>

   <p class="para">
    Verwenden Sie immer Anführungszeichen um ein
    Arrayindex-Zeichenkettenliteral. Beispielsweise ist
    <code class="literal">$foo[&#039;bar&#039;]</code> richtig, <code class="literal">$foo[bar]</code>
    hingegen nicht. Aber wieso? Es ist üblich, die folgende Schreibweise in
    alten Skripten zu finden:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">] = </span><span style="color: #DD0000">'enemy'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">// etc<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Dies ist falsch, funktioniert aber. Der Grund dafür ist, dass dieser Code
    eine undefinierte Konstante (<code class="literal">bar</code>) anstatt eines
    <span class="type"><a href="language.types.string.php" class="type String">String</a></span>s verwendet (<code class="literal">&#039;bar&#039;</code> - beachten Sie
    die Anführungszeichen). Dies funktioniert, da PHP einen
    <em>nackten String</em> (ein <span class="type"><a href="language.types.string.php" class="type String">String</a></span> ohne
    Anführungszeichen, der keinem bekannten Symbol entspricht) automatisch in
    einen <span class="type"><a href="language.types.string.php" class="type String">String</a></span> umwandelt, welcher den nackten
    <span class="type"><a href="language.types.string.php" class="type String">String</a></span> enthält. Beispielsweise wird PHP, wenn keine Konstante
    namens <strong><code>bar</code></strong> definiert ist, diese automatisch durch den
    <span class="type"><a href="language.types.string.php" class="type String">String</a></span> <code class="literal">&#039;bar&#039;</code> ersetzen und verwenden.
   </p>
   <div class="warning"><strong class="warning">Warnung</strong>
    <p class="simpara">
     Das Rückfallverhalten, eine nicht definierte Konstante als nackte
     Zeichenkette zu interpretieren, löst einen Fehler der Stufe
     <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> aus. Dies ist seit PHP 7.2.0 missbilligt
     und löst einen Fehler der Stufe <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> aus. Mit
     PHP 8.0.0 wurde es entfernt und löst eine
     <span class="classname"><a href="class.error.php" class="classname">Error</a></span>-Exception aus.
    </p>
   </div>

   <p class="simpara">
    Dies bedeutet nicht, dass man den Schlüssel <em>immer</em> in
    Anführungszeichen setzen muss. Setzen Sie keine Schlüssel in
    Anführungszeichen, welche
    <a href="language.constants.php" class="link">Konstanten</a> oder
    <a href="language.variables.php" class="link">Variablen</a> sind, da dies PHP
    daran hindern wird, diese zu interpretieren.
   </p>

   <div class="example" id="example-17">
    <p><strong>Beispiel #17 Anführungszeichen bei Schlüsseln</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'display_errors'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'html_errors'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Ein simples Array:<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">$count</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br />    echo </span><span style="color: #DD0000">"\nPrüfe </span><span style="color: #0000BB">$i</span><span style="color: #DD0000">: \n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Schlecht: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Gut: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Schlecht: </span><span style="color: #007700">{</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Gut: </span><span style="color: #007700">{</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <p class="para">Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Prüfe 0:
Notice: Undefined index:  $i in /path/to/script.html on line 9
Schlecht:
Gut: 1
Notice: Undefined index:  $i in /path/to/script.html on line 11
Schlecht:
Gut: 1

Prüfe 1:
Notice: Undefined index:  $i in /path/to/script.html on line 9
Schlecht:
Gut: 2
Notice: Undefined index:  $i in /path/to/script.html on line 11
Schlecht:
Gut: 2
</pre></div>
   </div>

   <p class="para">
    Weitere Beispiele zur Erläuterung dieses Verhaltens:
   </p>

   <div class="example" id="example-18">
    <p><strong>Beispiel #18 Weitere Beispiele</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Alle Fehler anzeigen<br /></span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'fruit' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'veggie' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Korrekt<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// apple<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// carrot<br /><br />// Inkorrekt. Dies Funktioniert nicht und PHP löst löst einen Error aus,<br />// da eine undefinierte Konstante namens fruit verwendet wird<br />//<br />// Error: Undefined constant "fruit"<br /></span><span style="color: #007700">try {<br />    echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">];<br />} catch (</span><span style="color: #0000BB">Error $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">), </span><span style="color: #DD0000">': '</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Dies definiert eine Konstante, um darzustellen, was hier passiert. Der Wert<br />// 'veggie' wird einer Konstanten namens fruit zugewiesen<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">, </span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Beachten Sie nun den Unterschied<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// apple<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// carrot<br /><br />// Hier ist es in Ordnung, da dies innerhalb eines Strings ist. Innerhalb eines<br />// Strings wird nicht nach Konstanten gesucht, weshalb kein Fehler auftritt<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;      </span><span style="color: #FF8000">// Hello apple<br /><br />// Mit einer Ausnahme: Klammern um ein Array sorgen dafür, dass Konstanten<br />// interpretiert werden<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// Hello carrot<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// Hello apple<br /><br />// Konkatenation (Verkettung) ist eine weitere Möglichkeit<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello " </span><span style="color: #007700">. </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello apple<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Dies wird nicht funktionieren und zu einem Parse-Fehler führen:<br />// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'<br />// Dies gilt natürlich ebenso für superglobale Werte innerhalb von Strings<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit']"</span><span style="color: #007700">;<br />print </span><span style="color: #DD0000">"Hello </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo']"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Wie im Abschnitt zur
    <a href="language.types.array.php#language.types.array.syntax" class="link">Syntax</a> gezeigt, muss
    der Inhalt der eckigen Klammern &#039;<code class="literal">[</code>&#039; und
    &#039;<code class="literal">]</code>&#039;) ein Ausdruck sein. Das bedeutet, dass folgendes
    funktioniert:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">somefunc</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Dieses Beispiel zeigt, wie der Rückgabewert einer Funktion als Arrayindex
    verwendet wird. PHP kann auch Konstanten auflösen:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_ERROR</span><span style="color: #007700">]   = </span><span style="color: #DD0000">"Ein fataler Fehler ist aufgetreten"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_WARNING</span><span style="color: #007700">] = </span><span style="color: #DD0000">"PHP hat eine Warnung ausgegeben"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_NOTICE</span><span style="color: #007700">]  = </span><span style="color: #DD0000">"Dies ist nur ein informeller Hinweis"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Es ist zu beachten, dass <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong> ein gültiger
    Bezeichner ist, genau wie <code class="literal">bar</code> im ersten Beispiel. Das
    vorherige Beispiel ist gleichbedeutend mit:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">"Ein fataler Fehler ist aufgetreten"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">] = </span><span style="color: #DD0000">"PHP hat eine Warnung ausgegeben"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">8</span><span style="color: #007700">] = </span><span style="color: #DD0000">"Dies ist nur ein informeller Hinweis"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    da <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong> gleich <code class="literal">1</code> ist usw.
   </p>

   <div class="sect4" id="language.types.array.foo-bar.why">
    <h5 class="title">Wieso ist es also schlecht?</h5>

    <p class="para">
     Irgendwann in der Zukunft könnte das PHP-Team eine weitere Konstante oder
     ein Schlüsselwort hinzufügen oder eine Konstante in anderem Code könnte
     Einfluss nehmen. Es ist beispielsweise bereits falsch, die Wörter
     <code class="literal">empty</code> oder <code class="literal">default</code> so zu schreiben,
     da dies <a href="reserved.php" class="link">reservierte Schlüsselwörter</a> sind.
    </p>

    <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
     <span class="simpara">
      Zur Wiederholung: innerhalb eines <span class="type"><a href="language.types.string.php" class="type String">String</a></span>s in doppelten
      Anführungszeichen ist es korrekt, einen Arrayschlüssel ohne
      Anführungszeichen zu schreiben, weswegen <code class="literal">&quot;$foo[bar]&quot;</code>
      zulässig ist. Die obigen Beispiele erklären dies ebenso, wie der
      Abschnitt zum
      <a href="language.types.string.php#language.types.string.parsing" class="link">Parsen von Variablen in Strings</a>.
     </span>
    </p></blockquote>

   </div>
  </div>
 </div>

 <div class="sect2" id="language.types.array.casting">
  <h3 class="title">Konvertierung in ein Array</h3>

  <p class="para">
   Für jeden der Typen <span class="type"><a href="language.types.integer.php" class="link">Integer</a></span>, <span class="type"><a href="language.types.float.php" class="link">Float</a></span>,
   <span class="type"><a href="language.types.string.php" class="link">String</a></span>, <span class="type"><a href="language.types.boolean.php" class="link">Boolean</a></span> und <span class="type"><a href="language.types.resource.php" class="link">Ressource</a></span>
   führt eine Konvertierung eines Wertes in ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> zu einem
   Array mit einem einzigen Wert mit dem Index Null und dem Wert des Skalaren,
   der konvertiert wurde. Anders ausgedrückt ist
   <code class="literal">(array) $scalarValue</code> exakt identisch mit
   <code class="literal">array($scalarValue)</code>.
  </p>

  <p class="para">
   Wenn ein <span class="type"><a href="language.types.object.php" class="link">Objekt</a></span> in ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> konvertiert wird,
   so ist das Ergebnis ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>, dessen Werte die Eigenschaften
   des <span class="type">Objekt</span>s sind. Die Schlüssel sind die Namen der
   Eigenschaften des Objektes mit ein paar nennenswerten Ausnahmen:
   Eigenschaften mit Ganzzahl-Namen sind nicht zugreifbar, privaten Variablen
   wird der Name der Klasse vorangestellt und protected Variablen wird dem
   Variablennamen ein &#039;*&#039; vorangestellt. Diese vorangestellten Werte haben
   <code class="literal">NUL</code>-Bytes auf beiden Seiten. Nicht-initialisierte
   <a href="language.oop5.properties.php#language.oop5.properties.typed-properties" class="link">typisierte Eigenschaften</a>
   werden stillschweigend verworfen.
  </p>

  <div class="example" id="example-19">
   <p><strong>Beispiel #19 Konvertierung in ein Array</strong></p>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$B</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$C</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">$D</span><span style="color: #007700">;<br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">1</span><span style="color: #007700">} = </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">((array) new </span><span style="color: #0000BB">A</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="annotation-interactive cdata"><pre>
array (
  &#039;&#039; . &quot;\0&quot; . &#039;A&#039; . &quot;\0&quot; . &#039;B&#039; =&gt; NULL,
  &#039;&#039; . &quot;\0&quot; . &#039;*&#039; . &quot;\0&quot; . &#039;C&#039; =&gt; NULL,
  &#039;D&#039; =&gt; NULL,
  1 =&gt; NULL,
)
</pre></div>
   </div>
  </div>

  <p class="para">
   Diese <code class="literal">NUL</code>-Bytes können zu einem unerwarteten Verhalten
   führen:
  </p>

  <div class="example" id="example-20">
   <p><strong>Beispiel #20 Umwandeln eines Objekts in ein Array</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$A</span><span style="color: #007700">; </span><span style="color: #FF8000">// Dies wird zu '\0A\0A'<br /></span><span style="color: #007700">}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$A</span><span style="color: #007700">; </span><span style="color: #FF8000">// Dies wird zu '\0B\0A'<br />    </span><span style="color: #007700">public </span><span style="color: #0000BB">$AA</span><span style="color: #007700">; </span><span style="color: #FF8000">// Dies wird zu 'AA'<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((array) new </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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
    <div class="annotation-interactive cdata"><pre>
array(3) {
  [&quot;BA&quot;]=&gt;
  NULL
  [&quot;AA&quot;]=&gt;
  NULL
  [&quot;AA&quot;]=&gt;
  NULL
}
</pre></div>
   </div>
  </div>

  <p class="para">
   Im obigen Beispiel scheint es zwei Schlüssel namens &#039;AA&#039; zu geben, obwohl
   einer davon eigentlich &#039;\0A\0A&#039; ist.
  </p>

  <p class="para">
   Die Konvertierung von <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> in ein <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> führt zu einem
   leeren <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>.
  </p>
 </div>

 <div class="sect2" id="language.types.array.comparing">
  <h3 class="title">Vergleiche</h3>

  <p class="para">
   Mit der Funktion <span class="function"><a href="function.array-diff.php" class="function">array_diff()</a></span> und mit
   <a href="language.operators.array.php" class="link">Array-Operatoren</a> ist es
   möglich, Arrays zu vergleichen.
  </p>
 </div>

 <div class="sect2" id="language.types.array.unpacking">
  <h3 class="title">Entpacken von Arrays</h3>

  <p class="para">
   Ein Array, dem ein <code class="code">...</code> vorangestellt ist, wird bei der
   Definition des Arrays an Ort und Stelle expandiert (entpackt). Nur Arrays
   und Objekte, die <span class="interfacename"><a href="class.traversable.php" class="interfacename">Traversable</a></span> implementieren,
   können entpackt werden. Das Entpacken von Arrays mit <code class="code">...</code> ist
   seit PHP 7.4.0 verfügbar. Dies wird auch als Spread-Operator bezeichnet.
  </p>

  <p class="para">
   Es ist möglich, ein Array mehrmals zu entpacken und normale Elemente vor
   oder nach dem Operator <code class="code">...</code> hinzuzufügen:

   <div class="example" id="example-21">
    <p><strong>Beispiel #21 Einfaches Entpacken von Arrays</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Verwendung der kurzen Array-Syntax.<br />// Funktioniert auch mit der array()-Syntax.<br /></span><span style="color: #0000BB">$arr1 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// [1, 2, 3]<br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= [</span><span style="color: #0000BB">0</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// [0, 1, 2, 3]<br /></span><span style="color: #0000BB">$arr4 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">, </span><span style="color: #0000BB">111</span><span style="color: #007700">]; </span><span style="color: #FF8000">// [1, 2, 3, 1, 2, 3, 111]<br /></span><span style="color: #0000BB">$arr5 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// [1, 2, 3, 1, 2, 3]<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">getArr</span><span style="color: #007700">() {<br />  return [</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">$arr6 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">getArr</span><span style="color: #007700">(), </span><span style="color: #DD0000">'c' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'d'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// ['a', 'b', 'c' =&gt; 'd']<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr2</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr3</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr4</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr5</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

  <p class="para">
   Das Entpacken eines Arrays mit dem Operator <code class="code">...</code> folgt der
   Syntax der Funktion <span class="function"><a href="function.array-merge.php" class="function">array_merge()</a></span>. Das heißt,
   nachfolgende Zeichenketten-Schlüssel überschreiben vorherige und
   Integer-Schlüssel werden neu nummeriert:

   <div class="example" id="example-22">
    <p><strong>Beispiel #22 Entpacken eines Arrays mit doppeltem Schlüssel</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Zeichenketten-Schlüssel<br /></span><span style="color: #0000BB">$arr1 </span><span style="color: #007700">= [</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= [</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= [</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">0</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr3</span><span style="color: #007700">); </span><span style="color: #FF8000">// ["a" =&gt; 2]<br /><br />// Integer-Schlüssel<br /></span><span style="color: #0000BB">$arr4 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr5 </span><span style="color: #007700">= [</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr6 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr4</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr6</span><span style="color: #007700">); </span><span style="color: #FF8000">// [1, 2, 3, 4, 5, 6]<br />// Das ergibt [0 =&gt; 1, 1 =&gt; 2, 2 =&gt; 3, 3 =&gt; 4, 4 =&gt; 5, 5 =&gt; 6]<br />// wobei die ursprünglichen Integer-Schlüssel nicht beibehalten wurden.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Schlüssel, die weder Integer noch Zeichenketten sind, lösen einen
    <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> aus. Solche Schlüssel können nur von
    einem <span class="interfacename"><a href="class.traversable.php" class="interfacename">Traversable</a></span>-Objekt erzeugt werden.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Vor PHP 8.1 wird das Entpacken eines Arrays, das einen String-Schlüssel
    enthält, nicht unterstützt:
   </p>
   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$arr1 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= [</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">4</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5<br /><br /></span><span style="color: #0000BB">$arr4 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr5 </span><span style="color: #007700">= [</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr6 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr4</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr5</span><span style="color: #007700">]; </span><span style="color: #FF8000">// works. [1, 2, 3, 4, 5]<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p></blockquote>

 </div>

 <div class="sect2" id="language.types.array.examples">
  <h3 class="title">Beispiele</h3>

  <p class="para">
   Der Array-Typ in PHP ist sehr vielseitig. Hier sind einige Beispiele:
  </p>

  <div class="example" id="example-23">
   <p><strong>Beispiel #23 Vielseitigkeit von Arrays</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Dies:<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array( </span><span style="color: #DD0000">'color' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'red'</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'taste' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'sweet'</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'shape' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'round'</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'name'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,<br />            </span><span style="color: #0000BB">4        </span><span style="color: #FF8000">// der Schlüssel wird 0 sein<br />          </span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'c'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// . . .ist komplett gleichbedeutend mit:<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'color'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'red'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'taste'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'sweet'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'shape'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'round'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]  = </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[]        = </span><span style="color: #0000BB">4</span><span style="color: #007700">;        </span><span style="color: #FF8000">// der Schlüssel wird 0 sein<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'c'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Nachdem der obige Code ausgeführt wurde, wird $a das Array<br />// array('color' =&gt; 'red', 'taste' =&gt; 'sweet', 'shape' =&gt; 'round',<br />// 'name' =&gt; 'apple', 0 =&gt; 4) sein und $b wird das Array<br />// array(0 =&gt; 'a', 1 =&gt; 'b', 2 =&gt; 'c') oder einfach array('a', 'b', 'c')<br />// sein<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</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>

  <div class="example" id="example-24">
   <p><strong>Beispiel #24 Verwendung von array()</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Array als Zuordnung von Eigenschaften<br /></span><span style="color: #0000BB">$map </span><span style="color: #007700">= array( </span><span style="color: #DD0000">'version'    </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />              </span><span style="color: #DD0000">'OS'         </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Linux'</span><span style="color: #007700">,<br />              </span><span style="color: #DD0000">'lang'       </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'englisch'</span><span style="color: #007700">,<br />              </span><span style="color: #DD0000">'short_tags' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">true<br />            </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$map</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Streng numerische Schlüssel<br />// Das ist gleichbedeutend mit array(0 =&gt; 7, 1 =&gt; 8, ...)<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array( </span><span style="color: #0000BB">7</span><span style="color: #007700">,<br />                </span><span style="color: #0000BB">8</span><span style="color: #007700">,<br />                </span><span style="color: #0000BB">0</span><span style="color: #007700">,<br />                </span><span style="color: #0000BB">156</span><span style="color: #007700">,<br />                -</span><span style="color: #0000BB">10<br />              </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$switching </span><span style="color: #007700">= array(         </span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #FF8000">// Schlüssel = 0<br />                    </span><span style="color: #0000BB">5    </span><span style="color: #007700">=&gt;  </span><span style="color: #0000BB">6</span><span style="color: #007700">,<br />                    </span><span style="color: #0000BB">3    </span><span style="color: #007700">=&gt;  </span><span style="color: #0000BB">7</span><span style="color: #007700">,<br />                    </span><span style="color: #DD0000">'a'  </span><span style="color: #007700">=&gt;  </span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />                            </span><span style="color: #0000BB">11</span><span style="color: #007700">, </span><span style="color: #FF8000">// Schlüssel = 6 (das Maximum der Integer-Schlüssel war 5)<br />                    </span><span style="color: #DD0000">'8'  </span><span style="color: #007700">=&gt;  </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #FF8000">// Schlüssel = 8 (Integer!)<br />                    </span><span style="color: #DD0000">'02' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">77</span><span style="color: #007700">, </span><span style="color: #FF8000">// Schlüssel = '02'<br />                    </span><span style="color: #0000BB">0    </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">12  </span><span style="color: #FF8000">// Der Wert 10 wird mit 12 überschrieben<br />                  </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$switching</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Leeres array<br /></span><span style="color: #0000BB">$empty </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$empty</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>

   </div>

  </div>

  <div class="example" id="language.types.array.examples.loop">
   <p><strong>Beispiel #25 Sammlung</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$farben </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'rot'</span><span style="color: #007700">, </span><span style="color: #DD0000">'blau'</span><span style="color: #007700">, </span><span style="color: #DD0000">'grün'</span><span style="color: #007700">, </span><span style="color: #DD0000">'gelb'</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$farben </span><span style="color: #007700">as </span><span style="color: #0000BB">$farbe</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Mögen sie </span><span style="color: #0000BB">$farbe</span><span style="color: #DD0000">?\n"</span><span style="color: #007700">;<br />}<br /><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="annotation-interactive cdata"><pre>
Mögen sie rot?
Mögen sie blau?
Mögen sie grün?
Mögen sie gelb?
</pre></div>
   </div>
  </div>

  <p class="para">
   Die Veränderung der Werte eines <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s ist mit der Übergabe
   als Referenz direkt möglich.
  </p>

  <div class="example" id="language.types.array.examples.changeloop">
   <p><strong>Beispiel #26 Elemente in der Schleife verändern</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$colors </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">, </span><span style="color: #DD0000">'blue'</span><span style="color: #007700">, </span><span style="color: #DD0000">'green'</span><span style="color: #007700">, </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$colors </span><span style="color: #007700">as &amp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$color </span><span style="color: #007700">= </span><span style="color: #0000BB">mb_strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />}<br />unset(</span><span style="color: #0000BB">$color</span><span style="color: #007700">); </span><span style="color: #FF8000">/* Sicherstellen, dass nachfolgende<br />Zugriffe auf $color nicht das letzte Element ändern */<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$colors</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="annotation-interactive cdata"><pre>
Array
(
    [0] =&gt; ROT
    [1] =&gt; BLAU
    [2] =&gt; GRÜN
    [3] =&gt; GELB
)
</pre></div>
   </div>
  </div>

  <p class="para">
   Dieses Beispiel erzeugt ein Array mit erstem Schlüssel 1.
  </p>

  <div class="example" id="example-25">
   <p><strong>Beispiel #27 1-basierte Indizes</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$firstquarter </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Januar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Februar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'März'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstquarter</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="annotation-interactive cdata"><pre>
Array
(
    [1] =&gt; Januar
    [2] =&gt; Februar
    [3] =&gt; März
)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-26">
   <p><strong>Beispiel #28 Füllen eines Arrays</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Dies füllt ein Array mit allen Einträgen eines Verzeichnisses<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">))) {<br />    </span><span style="color: #0000BB">$files</span><span style="color: #007700">[] = </span><span style="color: #0000BB">$file</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s sind geordnet. Die Reihenfolge kann durch verschiedene
   Sortierfunktionen verändert werden. Siehe
   <a href="ref.array.php" class="link">Array-Funktionen</a> für mehr Informationen.
   Die Funktion <span class="function"><a href="function.count.php" class="function">count()</a></span> kann verwendet werden, um die
   Anzahl der Elemente eines <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s zu ermitteln.
  </p>

  <div class="example" id="example-27">
   <p><strong>Beispiel #29 Ein Array sortieren</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   Da die Werte eines <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s beliebig sein können, können diese
   auch andere <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s sein. Dies ermöglicht die Erzeugung von
   rekursiven und mehrdimensionalen <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s.
  </p>

  <div class="example" id="example-28">
   <p><strong>Beispiel #30 Rekursive und mehrdimensionale Arrays</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fruits </span><span style="color: #007700">= array ( </span><span style="color: #DD0000">"fruits"  </span><span style="color: #007700">=&gt; array ( </span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,<br />                                       </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,<br />                                       </span><span style="color: #DD0000">"c" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"apple"<br />                                     </span><span style="color: #007700">),<br />                  </span><span style="color: #DD0000">"numbers" </span><span style="color: #007700">=&gt; array ( </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />                                       </span><span style="color: #0000BB">2</span><span style="color: #007700">,<br />                                       </span><span style="color: #0000BB">3</span><span style="color: #007700">,<br />                                       </span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />                                       </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />                                       </span><span style="color: #0000BB">6<br />                                     </span><span style="color: #007700">),<br />                  </span><span style="color: #DD0000">"holes"   </span><span style="color: #007700">=&gt; array (      </span><span style="color: #DD0000">"first"</span><span style="color: #007700">,<br />                                       </span><span style="color: #0000BB">5 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"second"</span><span style="color: #007700">,<br />                                            </span><span style="color: #DD0000">"third"<br />                                     </span><span style="color: #007700">)<br />                );<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Einige Beispiele für den Zugriff auf die Werte in den obigen Beispielen<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"holes"</span><span style="color: #007700">][</span><span style="color: #0000BB">5</span><span style="color: #007700">];    </span><span style="color: #FF8000">// gibt "second" aus<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"fruits"</span><span style="color: #007700">][</span><span style="color: #DD0000">"a"</span><span style="color: #007700">]; </span><span style="color: #FF8000">// gibt "orange" aus<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"holes"</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]);  </span><span style="color: #FF8000">// entfernt "first"<br /><br />// Ein neues mehrdimensionales Array erzeugen<br /></span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">][</span><span style="color: #DD0000">"green"</span><span style="color: #007700">] = </span><span style="color: #DD0000">"good"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$juices</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   Die Zuweisung eines <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s beinhaltet immer eine Kopie der
   Werte. Verwenden Sie den
   <a href="language.operators.php" class="link">Referenz-Operator</a>, um ein Array
   per Referenz zu kopieren.
  </p>

  <div class="example" id="example-29">
   <p><strong>Beispiel #31 Kopieren eines Arrays</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$arr1 </span><span style="color: #007700">= array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$arr1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr2</span><span style="color: #007700">[] = </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #FF8000">// $arr2 wurde geändert,<br />             // $arr1 ist weiterhin array(2, 3)<br /><br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr3</span><span style="color: #007700">[] = </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #FF8000">// nun sind $arr1 und $arr3 identisch<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr2</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

 </div>
</div><?php manual_footer($setup); ?>