PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » XML » XML_Serializer » Bug #3885

unserializing indexed arrays with one element

Details

Submitted2005-03-19 13:47 UTC
Fromgabest at gabest dot org
StatusBogus
PackageXML_Serializer
PHP Version4.3.2
Roadmaps(Not assigned)

Comments

[2005-03-19 13:47 UTC] gabest at gabest dot org

Description:
------------
I think this isn't really a bug, but rather something fudamentally wrong (maybe with my approach to the problem).

Let's say I have the following xml structure as a string:

<root>
<node>
<item><name>apple</name></item>
</node>
<node>
<item><name>orange</name></item>
<item><name>banana</name></item>
</node>
</root>

Then I try to unserialize this into a php array to display the item names with smarty. The problem is, it ends up in such an ambigous structure:

Array
(
[node] => Array
(
[0] => Array
(
[item] => Array
(
[name] => apple
)
)
[1] => Array
(
[item] => Array
(
[0] => Array
(
[name] => orange
)
[1] => Array
(
[name] => banana
)
)
)
)
)

Now, if I wanted to iterate this with smarty I'd need to write something like this ...

{foreach from=$node key=nindex item=n}
node {$nindex}<br>
{foreach from=$n.item key=index item=i}
{$index} => {$i.name}<br>
{/foreach}
{/foreach}

.. which would print this ...

node 0
name => a
node 1
0 => orange
1 => banana

... not exactly what I wished for.

Any idea how to avoid this situation? Is there a magic option to tell the unserializer what tags to treat as indexed arrays?

[2005-03-19 14:34 UTC] gabest at gabest dot org

Sorry, just stepped though the debugger and noticed it was the forceEnum option I needed. Would be nice to have a documentation on these things at least :)

[2005-03-20 18:06 UTC] gabest at gabest dot org

True... My mistake not noticing it!