Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.21.0

Bug #782 Array with one element becomes scalar
Submitted: 2004-02-18 02:38 UTC
From: joe-pear at 1db dot com Assigned:
Status: Bogus Package: XML_Serializer
PHP Version: 4.3.4 OS: Red Hat 9
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 5 + 12 = ?

 
 [2004-02-18 02:38 UTC] joe-pear at 1db dot com
Description: ------------ If an array has only 1 member, the unserialized data structure does not match the original structure. Instead of an array with 1 member, the class returns a string. I have to treat an array that may have 1 or more items different from a normal string or an array with 2 or more items. I would use attributes to force an array type, but there is no way to set attributes in an array without coding the xml seperately. Any suggestions? Reproduce code: --------------- $xmlser_options = array("indent" => " ","linebreak" => "\n","rootName" => $root, "rootAttributes" => array("version" => "0.91"),"typeHints" => false,"addDecl"=> true,"mode" =>"simplexml"); $xmlserializer = &new XML_Serializer($xmlser_options); $xmlserializer->serialize($array); $xml_string = $xmlserializer->getSerializedData(); $xmlunser_options = array("complexType" => "array","keyAttribute" => $key); $xmlunserializer = &new XML_Unserializer($xmlunser_options); $xmlunserializer->unserialize($xml_string, false); $array = $xmlunserializer->getUnserializedData(); Expected result: ---------------- $array = Array ( [group] => Array ( [0] => John ) ) serializes to: <?xml version="1.0"?> <groupupdate version="0.91"> <group>John</group> </groupupdate> and unserializes to an identical array: $array = Array ( [group] => Array ( [0] => John ) Actual result: -------------- $array = Array ( [group] => Array ( [0] => John ) ) serializes to: <?xml version="1.0"?> <groupupdate version="0.91"> <group>John</group> </groupupdate> and unserializes to an array containing a scalar string instead of the expected array: $array = Array ( [group] => John )

Comments

 [2004-02-18 08:15 UTC] schst
This is expected behaviour. If you want to use XML_Serializer/Unserializer as a replacement for serialize() and unserialize() you have to enable type hints in XML_Serializer. Otherwise XML_Unserializer cannot know that the group tag should always be treated as an array.