I have some XML that looks like this
$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>';
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
$xml_data = simplexml_load_string($xml_str);
foreach ($xml_data->FIELDS->FIELD as $field) {
foreach ($field->attributes() as $a => $b) {
$array[$b] = $field[0];
}
}
But I'm wondering if there is a better way?
TIA