note 113659 deleted from function.empty by cmb

From: Date: Sun, 01 Dec 2019 09:23:19 +0000
Subject: note 113659 deleted from function.empty by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213359@lists.php.net to get a copy of this message
Note Submitter: bharris at nospam dot epic dot com ---- XML::Unserializer produces unexpected results because empty($key) is true when $key = 0. Example: <?php include("/usr/share/php/XML/Unserializer.php"); $xml= <<<EOD <?xml version="1.0"?> <groups _type="array"> <XML_Serializer_Tag _originalKey="0" _type="array"> <gid _type="integer">10</gid> </XML_Serializer_Tag> <XML_Serializer_Tag _originalKey="5" _type="array"> <gid _type="integer">100</gid> </XML_Serializer_Tag> </groups> EOD; $uns = new XML_Unserializer(); $res = $uns->unserialize($xml); $recs = $uns->getUnserializedData(); print_r($recs); ?> produces Array ( [0] => Array ( [gid] => 10 ) [5] => Array ( [gid] => 100 ) ) while <?php include("/usr/share/php/XML/Unserializer.php"); $xml= <<<EOD <?xml version="1.0"?> <groups _type="array"> <XML_Serializer_Tag _originalKey="5" _type="array"> <gid _type="integer">100</gid> </XML_Serializer_Tag> <XML_Serializer_Tag _originalKey="0" _type="array"> <gid _type="integer">10</gid> </XML_Serializer_Tag> </groups> EOD; $uns = new XML_Unserializer(); $res = $uns->unserialize($xml); $recs = $uns->getUnserializedData(); print_r($recs); ?> produces Array ( [1] => Array ( [gid] => 100 ) [6] => Array ( [gid] => 10 ) ) This is because the "empty" key value of 0 results in a push onto the end of the array, rather than an insertion at key position 0.

« previous php.notes (#213359) next »