php xml parsing with attributes

I have some XML that looks like this\[code\]$xml_str = '<RESPONSE> <FIELDS> <FIELD KEY="A">1</FIELD> <FIELD KEY="B">2</FIELD> <FIELD KEY="C">3</FIELD> <FIELD KEY="D">4</FIELD> </FIELDS> </RESPONSE>';\[/code\]There is only ever going to be 1 "FIELDS" in the response. Is there a easy way I can put the "FIELD" elements in a array with the keys being the "KEY" and the value being the element value?I could do this\[code\]$xml_data = http://stackoverflow.com/questions/10668337/simplexml_load_string($xml_str);foreach ($xml_data->FIELDS->FIELD as $field) { foreach ($field->attributes() as $a => $b) { $array[$b] = $field[0]; }}\[/code\]But I'm wondering if there is a better way?TIA
 
Back
Top