I get product data via xml like that
JavaScript
x
<?xml version='1.0' encoding='UTF-8'?>
<brands>
<brand>
<name>Spielberger Mühle</name>
<kuerzel>SPI</kuerzel>
<id>e3124594-1d4c-4a72-8c66-37de394fece2</id>
<last_modified>27.04.2021 23:00:03</last_modified>
<logo>
<id>e3124594-1d4c-4a72-8c66-37de394fece2</id>
<name>SPI_Spielberger Mühle_e3124594-1d4c-4a72-8c66-37de394fece2.jpg</name>
<size>273981</size>
<mime_type>image/jpeg</mime_type>
<img_width>1533</img_width>
<img_height>676</img_height>
<last_modified>13.12.2016 12:48:02</last_modified>
</logo>
<url>www.spielberger-muehle.de</url>
<adr_invb_name>Spielberger GmbH</adr_invb_name>
<adr_invb_strasse>Burgermühle</adr_invb_strasse>
<adr_invb_plz>74336</adr_invb_plz>
<adr_invb_ort>Brackenheim</adr_invb_ort>
<adr_invb_land_id>8</adr_invb_land_id>
</brand>
</brands>
<producers>
<producer>
.
and in my php (shortened to the relevant parts)
JavaScript
$xml = simplexml_load_file('demo.xml');
foreach ($xml->brands->brand as $key => $value) {
print "Key: $key - Value: $valuen";
}
If I try to loop through the brand the following happens
JavaScript
Key: brand - Value:
edited because I figured I was way to verbose.
Advertisement
Answer
The reason this is happening is because of how SimpleXMLElement handles it’s iterator. You can iterate through the element using the get_object_vars
function.
JavaScript
foreach (get_object_vars($xml) as $key => $value) {
print "Key: $key - Value: $valuen";
}