Home » XML » XML_Serializer » Bug #782
Array with one element becomes scalar
Details
| Submitted | 2004-02-18 02:38 UTC |
|---|---|
| From | joe-pear at 1db dot com |
| Status | Bogus |
| Package | XML_Serializer |
| PHP Version | 4.3.4 |
| OS | Red Hat 9 |
| Roadmaps | (Not assigned) |
Comments
[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
)