I've found a strange issue when using unset function in combination with a SimpleXML decoded array. The unset function applied on one of the index of such an array (which is numeric-indexed), seems to reset the array index.
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?
Here's a test case that explains it.
<?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( <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node>1</node>
<node>2</node>
<node>3</node>
<node>4</node>
<node>5</node>
</root>
EOT
);
echo "SimpleXML array, before unset(\$a[1]): " . print_r( $xml, 1 );
unset( $xml->node[1] );
echo "SimpleXML array, after unset(\$a[1]): " . print_r( $xml, 1 );
The output that I get is,
Regular array, before unset($a[1]): Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Regular array, after unset($a[1]): Array
(
[0] => 1
[2] => 3
[3] => 4
)
SimpleXML array, before unset($a[1]): SimpleXMLElement Object
(
[node] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
)
SimpleXML array, after unset($a[1]): SimpleXMLElement Object
(
[node] => Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 5
)
)