In PHP, under what conditions does an unset() call reset the array index?
<p>I've found a strange issue when using <code>unset</code> function in combination with a SimpleXML decoded array. The <code>unset</code> function applied on one of the index of such an array (which is numeric-indexed), seems to reset the array index. </p>

<p>This seems like an undocumented behavior to me (or more like a bug). Has anyone got an explanation why there's a "special reindexing" treatment for SimpleXML arrays?</p>

<p>Here's a test case that explains it.</p>

<pre><code>&lt;?php
$a = array( 1, 2, 3, 4 );
echo "Regular array, before unset(\$a[1]): " . print_r( $a, 1 );
unset( $a[1] );
echo "Regular array, after unset(\$a[1]): " . print_r( $a, 1 );

$xml = simplexml_load_string( &lt;&lt;&lt;EOT
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;root&gt;
&lt;node&gt;1&lt;/node&gt;
&lt;node&gt;2&lt;/node&gt;
&lt;node&gt;3&lt;/node&gt;
&lt;node&gt;4&lt;/node&gt;
&lt;node&gt;5&lt;/node&gt;
&lt;/root&gt;
EOT
 );

echo "SimpleXML array, before unset(\$a[1]): " . print_r( $xml, 1 );
unset( $xml-&gt;node[1] );
echo "SimpleXML array, after unset(\$a[1]): " . print_r( $xml, 1 );
</code></pre>

<p>The output that I get is,</p>

<pre><code>Regular array, before unset($a[1]): Array
(
    [0] =&gt; 1
    [1] =&gt; 2
    [2] =&gt; 3
    [3] =&gt; 4
)
Regular array, after unset($a[1]): Array
(
    [0] =&gt; 1
    [2] =&gt; 3
    [3] =&gt; 4
)
SimpleXML array, before unset($a[1]): SimpleXMLElement Object
(
    [node] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; 2
            [2] =&gt; 3
            [3] =&gt; 4
            [4] =&gt; 5
        )

)
SimpleXML array, after unset($a[1]): SimpleXMLElement Object
(
    [node] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; 3
            [2] =&gt; 4
            [3] =&gt; 5
        )

)
</code></pre>
