Jithesh Kt
Jithesh Kt

Reputation: 2043

php simple xml child node into a foreach

I have an XML file. It looks like the following :

<Offers>
 <Offer>
  <OfferID>1</OfferID>
  <OfferName>Offer One</OfferName>
 </Offer>
 <Offer>
  <OfferID>2</OfferID>
  <OfferName>Offer Two</OfferName>
 </Offer>
</Offers>

I have a variable declared like below :

$latest_offers = simplexml_load_file("Offers.xml");

Now my problem is how i can get a out put like this :

foreach($offer as $item)
{
 echo <offerID>, <OfferName>...each and everything inside the offer nod
}

How can i achieve this. Tried a lot but no success.

Upvotes: 0

Views: 3233

Answers (1)

web-nomad
web-nomad

Reputation: 6003

Try this.

<?php
$latest_offers = simplexml_load_file("Offers.xml");
foreach($latest_offers->Offer as $offer) {
   echo $offer->offerID . ', ' . $offer->OfferName . '<br>';
}
?>

Upvotes: 1

Related Questions