gaarf/XML-string-to-PHP-array

Ambiguous conversion of sub elements

Closed this issue · 1 comments

Hi,

I will use the example provided:

<tv>
  <show name="Family Guy">
    <dog>Brian</dog>
    <kid>Chris</kid>
    <kid>Meg</kid>
  </show>
</tv>

This is converted to:

Array
(
    [show] => Array
        (
            [@attributes] => Array
                (
                    [name] => Family Guy
                )
            [dog] => Brian
            [kid] => Array
                (
                    [0] => Chris
                    [1] => Meg
                )
        )
)

If there are multiple sub-elements (kid - Chris and Meg), it spits out an array of values. The problem is that if there is only one sub-element (dog), its value is not an array but the value directly. This is hard to handle when your code needs to work with the data.

Should it not always be an array?

Thx,

smuellerus

Changing it to always be an array would indeed seem more logical, but it would no longer be "output compatible" with the original hack json_decode(json_encode((array) simplexml_load_string($s)),1);