PHP Xml to Array conversion

sewrobb

New Member
So im trying to convert a XML string using php function that passes it through simplexml_load_string()the function:\[code\]function XML2Array ( $xml , $recursive = false){ if (!$recursive) $array = simplexml_load_string($xml); else $array = $xml; $newArray = array () ; $array = (array)$array ; foreach($array as $key => $value){ $value = http://stackoverflow.com/questions/15846220/(array)$value; if (isset($value[ 0 ])) $newArray[$key]=trim($value[0]); else $newArray[$key] = XML2Array($value, true); } return $newArray;}\[/code\]When I attempt to pass a XML string that has multiple same level nodes like\[code\]<Teams> <Team> <Color>Blue Team</Color> </Team> <Team> <Color>Red Team</Color> </Team></Teams>\[/code\]It will only return\[code\][Teams] => Array ( [Team] => )\[/code\]But if I do a XML string with only one the nodes it returns\[code\][Teams] => Array ( [Team] => Array ( [Color] => Red Team ) )\[/code\]
 
Back
Top