Does anyone know if I can force a SimpleXMLElement variable to always be an array?
<result>
<item>
<id>1</id>
<name>name 1</name>
</item>
<item>
<id>2</id>
<name>name 2</name>
</item>
</result>
When parsing the above XML through simplexml_load_string() I get the following object
SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[id] => 1
[name] => name 1
)
[1] => SimpleXMLElement Object
(
[id] => 2
[name] => name 2
)
)
)
Which is great because I can loop through "item" and get the individual objects. But when item is a single entry, like below, I get a different object structure which ruins my loop becuase I start looping through "id" and "name" instead of objects.
<result>
<item>
<id>1</id>
<name>name 1</name>
</item>
</result>
SimpleXMLElement Object
(
[item] => SimpleXMLElement Object
(
[id] => 1
[name] => name 1
)
)
Is there anyway to force "item" to be an array with simplexml_load_string() or by re-structuring the XML?