0

我有一个 XML 文件。它如下所示:

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

我有一个声明如下的变量:

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

现在我的问题是我怎样才能得到这样的输出:

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

我怎样才能做到这一点。尝试了很多但没有成功。

4

1 回答 1

1

试试这个。

<?php
$latest_offers = simplexml_load_file("Offers.xml");
foreach($latest_offers->Offer as $offer) {
   echo $offer->offerID . ', ' . $offer->OfferName . '<br>';
}
?>
于 2012-11-09T07:11:11.073 回答