parsing xml force array structure when there are 1 or more children

I am trying to parse some XML which has the following structures (2 forms shown, both valid):\[code\]<xml-structure> <parent-group> <child> <element1>Some Data</element1> <element2>Some Data</element2> </child> </parent-group></xml-structure><xml-structure> <parent-group> <child> <element1>Some Data</element1> <element2>Some Data</element2> </child> <child> <element1>Some Data</element1> <element2>Some Data</element2> </child> </parent-group></xml-structure>\[/code\]The following is used to get this entire structure into an associative array and look at the data:\[code\]$arr = json_decode(json_encode((array) simplexml_load_string($xml)),1);$children = $arr['parent-group']['child'];\[/code\]I want to iterate over the children to look at the data - however, the following works only if there are multiple children:\[code\]foreach($children as $child) { echo $child['element1'];}\[/code\]If there is only one child, then that does not work, since the following is then true:\[code\]$children === {'element1' => 'Some Data', 'element2' => 'Some Data'}\[/code\]Without knowing in advance whether there are multiple children or not, how can I make sure that \[code\]$children\[/code\] will always be an array (perhaps of size 1)?
 
Back
Top