<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.types.array.php',
    1 => 'Arrays',
    2 => 'Arrays',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'prev' => 
  array (
    0 => 'language.types.numeric-strings.php',
    1 => 'Numeric strings',
  ),
  'next' => 
  array (
    0 => 'language.types.object.php',
    1 => 'Objects',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">
  For a list of all array functions, see the <a href="ref.array.php" class="link">array functions</a> chapter in the documentation.
 </p>

 <p class="para">
  An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> in PHP is actually an ordered map. A map is a type that
  associates <em>values</em> to <em>keys</em>. This type
  is optimized for several different uses; it can be treated as an array,
  list (vector), hash table (an implementation of a map), dictionary,
  collection, stack, queue, and probably more. As <span class="type"><a href="language.types.array.php" class="type array">array</a></span> values can
  be other <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s, trees and multidimensional <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s
  are also possible.
 </p>

 <p class="para">
  Explanation of those data structures is beyond the scope of this manual, but
  at least one example is provided for each of them. For more information, look
  towards the considerable literature that exists about this broad topic.
 </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">Specifying with <span class="function"><a href="function.array.php" class="function">array()</a></span></h4>

   <p class="para">
    An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be created using the <span class="function"><a href="function.array.php" class="function">array()</a></span>
    language construct. It takes any number of comma-separated
    <code class="literal"><span class="replaceable">key</span> =&gt; <span class="replaceable">value</span></code> pairs
    as arguments.
   </p>

   <pre class="synopsis">
array(
    <span class="optional"><span class="replaceable">key</span>  =&gt; </span><span class="replaceable">value</span>,
    <span class="optional"><span class="replaceable">key2</span> =&gt; </span><span class="replaceable">value2</span>,
    <span class="optional"><span class="replaceable">key3</span> =&gt; </span><span class="replaceable">value3</span>,
    ...
)</pre>
   

   <p class="para">
    The comma after the last array element is optional and can be omitted. This is usually done
    for single-line arrays, i.e. <code class="literal">array(1, 2)</code> is preferred over
    <code class="literal">array(1, 2, )</code>. For multi-line arrays on the other hand the trailing comma
    is commonly used, as it allows easier addition of new elements at the end.
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     A short array syntax exists which replaces
     <code class="literal">array()</code> with <code class="literal">[]</code>.
    </p>
   </p></blockquote>

   <div class="example" id="example-1">
    <p><strong>Example #1 A simple 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">// Using the short 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">
    The <span class="replaceable">key</span> can either be an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
    or a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. The <span class="replaceable">value</span> can be
    of any type.
   </p>

   <p class="para" id="language.types.array.key-casts">
    Additionally the following <span class="replaceable">key</span> casts will occur:
    <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 containing valid decimal <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s, unless the number is preceded by a <code class="literal">+</code> sign, will be cast to the
       <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type. E.g. the key <code class="literal">&quot;8&quot;</code> will actually be
       stored under <code class="literal">8</code>. On the other hand <code class="literal">&quot;08&quot;</code> will
       not be cast, as it isn&#039;t a valid decimal integer.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.float.php" class="type Float">Float</a></span>s are also cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s, which means that the
       fractional part will be truncated. E.g. the key <code class="literal">8.7</code> will actually
       be stored under <code class="literal">8</code>.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.boolean.php" class="type Bool">Bool</a></span>s are cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s, too, i.e. the key
       <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> will actually be stored under <code class="literal">1</code>
       and the key <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> under <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> will be cast to the empty string, i.e. the key
       <code class="literal">null</code> will actually be stored under <code class="literal">&quot;&quot;</code>.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s and <span class="type"><a href="language.types.object.php" class="type object">object</a></span>s <em>can not</em> be used as keys.
       Doing so will result in a warning: <code class="literal">Illegal offset type</code>.
      </span>
     </li>
    </ul>
   </p>

   <p class="para">
    If multiple elements in the array declaration use the same key, only the last one
    will be used as all others are overwritten.
   </p>

   <div class="example" id="example-2">
    <p><strong>Example #2 Type Casting and Overwriting example</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>The above example will output:</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>
     As all the keys in the above example are cast to <code class="literal">1</code>, the value will be overwritten
     on every new element and the last assigned value <code class="literal">&quot;d&quot;</code> is the only one left over.
    </p></div>
   </div>

   <p class="para">
    PHP arrays can contain <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span> keys at the same time
    as PHP does not distinguish between indexed and associative arrays.
   </p>

   <div class="example" id="example-3">
    <p><strong>Example #3 Mixed <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span> keys</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>The above example will output:</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">
    The <span class="replaceable">key</span> is optional. If it is not specified, PHP will
    use the increment of the largest previously used <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> key.
   </p>

   <div class="example" id="example-4">
    <p><strong>Example #4 Indexed arrays without key</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>The above example will output:</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">
    It is possible to specify the key only for some elements and leave it out for others:
   </p>

   <div class="example" id="example-5">
    <p><strong>Example #5 Keys not on all elements</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>The above example will output:</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>
     As you can see the last value <code class="literal">&quot;d&quot;</code> was assigned the key
     <code class="literal">7</code>. This is because the largest integer key before that
     was <code class="literal">6</code>.
    </p></div>
   </div>

   <div class="example" id="example-6">
    <p><strong>Example #6 Complex Type Casting and Overwriting example</strong></p>
    <div class="example-contents"><p>
     This example includes all variations of type casting of keys and overwriting
     of elements.
    </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">// the value "a" will be overwritten by "b"<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">// the value "b" will be overwritten by "c"<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">// as this is not an integer string it will NOT override the key for 1<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">// as this is not an integer string it will NOT override the key for 1<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">// the value "c" will be overwritten by "g"<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">// the value "i" will be overwritten by "j"<br />    </span><span style="color: #DD0000">'k'</span><span style="color: #007700">, </span><span style="color: #FF8000">// value "k" is assigned the key 2. This is because the largest integer key before that was 1<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">// the value "k" will be overwritten by "l"<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>The above example will output:</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>Example #7 Negative index example</strong></p>
    <div class="example-contents"><p>
     When assigning a negative integer key <code class="literal">n</code>, PHP will take care to
     assign the next key to <code class="literal">n+1</code>.
    </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>The above example will output:</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">Warning</strong>
     <p class="simpara">
      Prior to PHP 8.3.0, assigning a negative integer key <code class="literal">n</code> would
      assign the next key to <code class="literal">0</code>, the previous example would
      therefore output:
     </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">Accessing array elements with square bracket syntax</h4>

   <p class="para">
    Array elements can be accessed using the <code class="literal">array[key]</code> syntax.
   </p>

   <div class="example" id="example-8">
    <p><strong>Example #8 Accessing array elements</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>The above example will output:</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">Note</strong>: 
    <p class="para">
     Prior to PHP 8.0.0, square brackets and curly braces could be used interchangeably
     for accessing array elements (e.g. <code class="literal">$array[42]</code> and <code class="literal">$array{42}</code>
     would both do the same thing in the example above).
     The curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.
    </p>
   </p></blockquote>

   <div class="example" id="example-9">
    <p><strong>Example #9 Array dereferencing</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">Note</strong>: 
    <p class="para">
      Attempting to access an array key which has not been defined is
      the same as accessing any other undefined variable:
      an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>-level error message
      (<strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>-level prior to PHP 8.0.0) will be
      issued, and the result will be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Array dereferencing a scalar value which is not a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
     yields <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. Prior to PHP 7.4.0, that did not issue an error message.
     As of PHP 7.4.0, this issues <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>;
     as of PHP 8.0.0, this issues <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">Creating/modifying with square bracket syntax</h4>

   <p class="para">
    An existing <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be modified by explicitly setting values
    in it.
   </p>

   <p class="para">
    This is done by assigning values to the <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, specifying the
    key in brackets. The key can also be omitted, resulting in an empty pair of
    brackets (<code class="literal">[]</code>).
   </p>

   <pre class="synopsis">
$arr[<span class="replaceable">key</span>] = <span class="replaceable">value</span>;
$arr[] = <span class="replaceable">value</span>;
// <span class="replaceable">key</span> may be an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> or <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
// <span class="replaceable">value</span> may be any value of any type</pre>

   <p class="para">
    If <var class="varname">$arr</var> doesn&#039;t exist yet or is set to <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, it will be created, so this is
    also an alternative way to create an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. This practice is
    however discouraged because if <var class="varname">$arr</var> already contains
    some value (e.g. <span class="type"><a href="language.types.string.php" class="type string">string</a></span> from request variable) then this
    value will stay in the place and <code class="literal">[]</code> may actually stand
    for <a href="language.types.string.php#language.types.string.substr" class="link">string access
    operator</a>. It is always better to initialize a variable by a direct
    assignment.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     As of PHP 7.1.0, applying the empty index operator on a string throws a fatal
     error. Formerly, the string was silently converted to an array.
    </span>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     As of PHP 8.1.0, creating a new array from <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> value is deprecated.
     Creating a new array from <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> and undefined values is still allowed.
    </span>
   </p></blockquote>

   <p class="para">
    To change a certain
    value, assign a new value to that element using its key. To remove a
    key/value pair, call the <span class="function"><a href="function.unset.php" class="function">unset()</a></span> function on it.
   </p>

   <div class="example" id="example-10">
    <p><strong>Example #10 Using Square Brackets with 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">// This is the same as $arr[13] = 56;<br />                // at this point of the script<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">// This adds a new element to<br />                // the array with key "x"<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">// This removes the element from the 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">// This deletes the whole 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">Note</strong>: 
    <p class="para">
     As mentioned above, if no key is specified, the maximum of the existing
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> indices is taken, and the new key will be that maximum
     value plus 1 (but at least 0). If no <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> indices exist yet, the key will
     be <code class="literal">0</code> (zero).
    </p>

    <p class="para">
     Note that the maximum integer key used for this <em>need not
     currently exist in the <span class="type"><a href="language.types.array.php" class="type array">array</a></span></em>. It need only have
     existed in the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> at some time since the last time the
     <span class="type"><a href="language.types.array.php" class="type array">array</a></span> was re-indexed. The following example illustrates:
    </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">// Now delete every item, but leave the array itself intact:<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">// Append an item (note that the new key is 5, instead of 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">// Re-index:<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">The above example will output:</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">Array destructuring</h4>

   <p class="para">
    Arrays can be destructured using the <code class="literal">[]</code> (as of PHP 7.1.0) or
    <span class="function"><a href="function.list.php" class="function">list()</a></span> language constructs. These
    constructs can be used to destructure an array into distinct variables.
   </p>

   <div class="example" id="example-11">
    <p><strong>Example #11 Array Destructuring</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">// prints "foo"<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">// prints "bar"<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">// prints "baz"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Array destructuring can be used in <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> to destructure
    a multi-dimensional array while iterating over it.
   </p>

   <div class="example" id="example-12">
    <p><strong>Example #12 Array Destructuring 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 elements will be ignored if the variable is not provided. Array
    destructuring always starts at index <code class="literal">0</code>.
   </p>

   <div class="example" id="example-13">
    <p><strong>Example #13 Ignoring Elements</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">// Assign the element at index 2 to the 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">// prints "baz"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    As of PHP 7.1.0, associative arrays can be destructured too. This also
    allows for easier selection of the right element in numerically indexed
    arrays as the index can be explicitly specified.
   </p>

   <div class="example" id="example-14">
    <p><strong>Example #14 Destructuring Associative 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">// Assign the element at index 'baz' to the 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">// prints 3<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">// Assign the element at index 2 to the 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">// prints "baz"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Array destructuring can be used for easy swapping of two variables.
   </p>

   <div class="example" id="example-15">
    <p><strong>Example #15 Swapping Two Variable</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">// prints 2<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">// prints 1<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      The spread operator (<code class="literal">...</code>) is not supported in assignments.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      Attempting to access an array key which has not been defined is
      the same as accessing any other undefined variable:
      an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>-level error message
      (<strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>-level prior to PHP 8.0.0) will be
      issued, and the result will be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      Destructuring a scalar value assigns <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to all variables.
    </p>
   </p></blockquote>
  </div>

 </div>

 <div class="sect2" id="language.types.array.useful-funcs">
  <h3 class="title">Useful functions</h3>

  <p class="para">
   There are quite a few useful functions for working with arrays. See the
   <a href="ref.array.php" class="link">array functions</a> section.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The <span class="function"><a href="function.unset.php" class="function">unset()</a></span> function allows removing keys from an
    <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. Be aware that the array will <em>not</em> be
    reindexed. If a true &quot;remove and shift&quot; behavior is desired, the
    <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be reindexed using the
    <span class="function"><a href="function.array-values.php" class="function">array_values()</a></span> function.
   </p>

   <div class="example" id="example-16">
    <p><strong>Example #16 Unsetting Intermediate Elements</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">/* will produce an array that would have been defined as<br />   $a = array(1 =&gt; 'one', 3 =&gt; 'three');<br />   and NOT<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">// Now $b is 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">
   The <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> control
   structure exists specifically for <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s. It provides an easy
   way to traverse an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
  </p>
 </div>

 <div class="sect2" id="language.types.array.donts">
  <h3 class="title">Array do&#039;s and don&#039;ts</h3>

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

   <p class="para">
    Always use quotes around a string literal array index. For example,
    <code class="literal">$foo[&#039;bar&#039;]</code> is correct, while
    <code class="literal">$foo[bar]</code> is not. But why? It is common to encounter this
    kind of syntax in old scripts:
   </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">
    This is wrong, but it works. The reason is that this code has an undefined
    constant (<code class="literal">bar</code>) rather than a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (<code class="literal">&#039;bar&#039;</code> - notice the
    quotes). It works because PHP automatically converts a
    <em>bare string</em> (an unquoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span> which does
    not correspond to any known symbol) into a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> which
    contains the bare <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. For instance, if there is no defined
    constant named <strong><code>bar</code></strong>, then PHP will substitute in the
    <span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="literal">&#039;bar&#039;</code> and use that.
   </p>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     The fallback to treat an undefined constant as bare string issues an error
     of level <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>.
     This has been deprecated as of PHP 7.2.0, and issues an error
     of level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
     As of PHP 8.0.0, it has been removed and throws an
     <span class="classname"><a href="class.error.php" class="classname">Error</a></span> exception.
    </p>
   </div>

   <p class="simpara">
    This does not mean to <em>always</em> quote the key. Do not
    quote keys which are <a href="language.constants.php" class="link">constants</a> or
    <a href="language.variables.php" class="link">variables</a>, as this will prevent
    PHP from interpreting them.
   </p>

   <div class="example" id="example-17">
    <p><strong>Example #17 Key Quoting</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">// 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">);<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">"\nChecking </span><span style="color: #0000BB">$i</span><span style="color: #DD0000">: \n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"Bad: " </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">"Good: " </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">"Bad: </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">"Good: </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">The above example will output:</p>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Checking 0:
Notice: Undefined index:  $i in /path/to/script.html on line 9
Bad:
Good: 1
Notice: Undefined index:  $i in /path/to/script.html on line 11
Bad:
Good: 1

Checking 1:
Notice: Undefined index:  $i in /path/to/script.html on line 9
Bad:
Good: 2
Notice: Undefined index:  $i in /path/to/script.html on line 11
Bad:
Good: 2
</pre></div>
   </div>

   <p class="para">
    More examples to demonstrate this behaviour:
   </p>

   <div class="example" id="example-18">
    <p><strong>Example #18 More Examples</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">// Show all errors<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">// Correct<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 />// Incorrect. This does not work and throws a PHP Error because<br />// of an undefined constant named fruit<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">// This defines a constant to demonstrate what's going on.  The value 'veggie'<br />// is assigned to a constant named fruit.<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">// Notice the difference now<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 />// The following is okay, as it's inside a string. Constants are not looked for<br />// within strings, so no error occurs here<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 />// With one exception: braces surrounding arrays within strings allows constants<br />// to be interpreted<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 />// Concatenation is another option<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">// This will not work, and will result in a parse error, such as:<br />// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'<br />// This of course applies to using superglobals in strings as well<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">
    As stated in the <a href="language.types.array.php#language.types.array.syntax" class="link">syntax</a>
    section, what&#039;s inside the square brackets (&#039;<code class="literal">[</code>&#039; and
    &#039;<code class="literal">]</code>&#039;) must be an expression. This means that code like
    this works:
   </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">
    This is an example of using a function return value as the array index. PHP
    also knows about constants:
   </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">"A fatal error has occurred"</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 issued a warning"</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">"This is just an informal notice"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Note that <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong> is also a valid identifier, just like
    <code class="literal">bar</code> in the first example. But the last example is in fact
    the same as writing:
   </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">"A fatal error has occurred"</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 issued a warning"</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">"This is just an informal notice"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    because <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong> equals <code class="literal">1</code>, etc.
   </p>

   <div class="sect4" id="language.types.array.foo-bar.why">
    <h5 class="title">So why is it bad then?</h5>

    <p class="para">
     At some point in the future, the PHP team might want to add another
     constant or keyword, or a constant in other code may interfere. For
     example, it is already wrong to use the words <code class="literal">empty</code> and
     <code class="literal">default</code> this way, since they are
     <a href="reserved.php" class="link">reserved keywords</a>.
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      To reiterate, inside a double-quoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, it&#039;s valid to
      not surround array indexes with quotes so <code class="literal">&quot;$foo[bar]&quot;</code>
      is valid. See the above examples for details on why as well as the section
      on <a href="language.types.string.php#language.types.string.parsing" class="link">variable parsing in
      strings</a>.
     </span>
    </p></blockquote>

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

 <div class="sect2" id="language.types.array.casting">
  <h3 class="title">Converting to array</h3>

  <p class="para">
   For any of the types <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, <span class="type"><a href="language.types.float.php" class="type float">float</a></span>,
   <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> and <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>,
   converting a value to an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> results in an array with a single
   element with index zero and the value of the scalar which was converted. In
   other words, <code class="code">(array) $scalarValue</code> is exactly the same as
   <code class="literal">array($scalarValue)</code>.
  </p>

  <p class="para">
   If an <span class="type"><a href="language.types.object.php" class="type object">object</a></span> is converted to an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, the result
   is an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> whose elements are the <span class="type"><a href="language.types.object.php" class="type object">object</a></span>&#039;s
   properties. The keys are the member variable names, with a few notable
   exceptions: integer properties are unaccessible;
   private variables have the class name prepended to the variable
   name; protected variables have a &#039;*&#039; prepended to the variable name. These
   prepended values have <code class="literal">NUL</code> bytes on either side.
   Uninitialized <a href="language.oop5.properties.php#language.oop5.properties.typed-properties" class="link">typed properties</a>
   are silently discarded.
  </p>

  <div class="example" id="example-19">
   <p><strong>Example #19 Converting to an 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>The above example will output:</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">
   These <code class="literal">NUL</code> can result in some unexpected behaviour:
  </p>

  <div class="example" id="example-20">
   <p><strong>Example #20 Casting an Object to an 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">// This will become '\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">// This will become '\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">// This will become '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>The above example will output:</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">
   The above will appear to have two keys named &#039;AA&#039;, although one of them is
   actually named &#039;\0A\0A&#039;.
  </p>

  <p class="para">
   Converting <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> results in an empty
   <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">Comparing</h3>

  <p class="para">
   It is possible to compare arrays with the <span class="function"><a href="function.array-diff.php" class="function">array_diff()</a></span>
   function and with
   <a href="language.operators.array.php" class="link">array operators</a>.
  </p>
 </div>

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

  <p class="para">
   An array prefixed by <code class="code">...</code> will be expanded in place during array definition.
   Only arrays and objects which implement <span class="interfacename"><a href="class.traversable.php" class="interfacename">Traversable</a></span> can be expanded.
   Array unpacking with <code class="code">...</code> is available as of PHP 7.4.0. This is also called
   the spread operator.
  </p>

  <p class="para">
   It&#039;s possible to expand multiple times, and add normal elements before or after the <code class="code">...</code> operator:

   <div class="example" id="example-21">
    <p><strong>Example #21 Simple array unpacking</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">// Using short array syntax.<br />// Also, works with 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">
   Unpacking an array with the <code class="code">...</code> operator follows the semantics of the <span class="function"><a href="function.array-merge.php" class="function">array_merge()</a></span> function.
   That is, later string keys overwrite earlier ones and integer keys are renumbered:

   <div class="example" id="example-22">
    <p><strong>Example #22 Array unpacking with duplicate key</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">// string key<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 key<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 />// Which is [0 =&gt; 1, 1 =&gt; 2, 2 =&gt; 3, 3 =&gt; 4, 4 =&gt; 5, 5 =&gt; 6]<br />// where the original integer keys have not been retained.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Keys that are neither integers nor strings throw a <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span>.
    Such keys can only be generated by a <span class="interfacename"><a href="class.traversable.php" class="interfacename">Traversable</a></span> object.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Prior to PHP 8.1, unpacking an array which has a string key is not supported:
   </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">Examples</h3>

  <p class="para">
   The array type in PHP is very versatile. Here are some examples:
  </p>

  <div class="example" id="example-23">
   <p><strong>Example #23 Array Versatility</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">// This:<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">// key will be 0<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">// . . .is completely equivalent with this:<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">// key will be 0<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">// After the above code is executed, $a will be the array<br />// array('color' =&gt; 'red', 'taste' =&gt; 'sweet', 'shape' =&gt; 'round',<br />// 'name' =&gt; 'apple', 0 =&gt; 4), and $b will be the array<br />// array(0 =&gt; 'a', 1 =&gt; 'b', 2 =&gt; 'c'), or simply array('a', 'b', 'c').<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>Example #24 Using 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 as (property-)map<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">'english'</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">// strictly numerical keys<br />// this is the same as 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">// key = 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">// key = 6 (maximum of integer-indices was 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">// key = 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">// key = '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">// the value 10 will be overwritten by 12<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">// empty 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>Example #25 Collection</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 </span><span style="color: #0000BB">$color</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Do you like </span><span style="color: #0000BB">$color</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>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
</pre></div>
   </div>
  </div>

  <p class="para">
   Changing the values of the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> directly is possible
   by passing them by reference.
  </p>

  <div class="example" id="language.types.array.examples.changeloop">
   <p><strong>Example #26 Changing element in the loop</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">/* ensure that following writes to<br />$color will not modify the last array element */<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>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
    [0] =&gt; RED
    [1] =&gt; BLUE
    [2] =&gt; GREEN
    [3] =&gt; YELLOW
)
</pre></div>
   </div>
  </div>

  <p class="para">
   This example creates a one-based array.
  </p>

  <div class="example" id="example-25">
   <p><strong>Example #27 One-based index</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">'January'</span><span style="color: #007700">, </span><span style="color: #DD0000">'February'</span><span style="color: #007700">, </span><span style="color: #DD0000">'March'</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>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
    [1] =&gt; January
    [2] =&gt; February
    [3] =&gt; March
)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-26">
   <p><strong>Example #28 Filling an 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">// fill an array with all items from a directory<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 are ordered. The order can be changed using various
   sorting functions. See the <a href="ref.array.php" class="link">array functions</a>
   section for more information. The <span class="function"><a href="function.count.php" class="function">count()</a></span> function can be
   used to count the number of items in an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
  </p>

  <div class="example" id="example-27">
   <p><strong>Example #29 Sorting an array</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">
   Because the value of an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be anything, it can also be
   another <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. This enables the creation of recursive and
   multi-dimensional <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>Example #30 Recursive and multi-dimensional 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">// Some examples to address values in the array above<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">// prints "second"<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">// prints "orange"<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">// remove "first"<br /><br />// Create a new multi-dimensional array<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">
   <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> assignment always involves value copying. Use the
   <a href="language.operators.php" class="link">reference operator</a> to copy an
   <span class="type"><a href="language.types.array.php" class="type array">array</a></span> by reference.
  </p>

  <div class="example" id="example-29">
   <p><strong>Example #31 Array Copying</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 is changed,<br />             // $arr1 is still 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">// now $arr1 and $arr3 are the same<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); ?>