PDA

View Full Version : Parsing XML & PHP


nealw
04-01-2007, 10:55 PM
Newbie to XML but I have a parser put together that ouputs the array below. What I am having problems with is getting the values into php friendy variables so I can display the content. I am also including a "work in progress" piece of code where I am trying to loop through the array to get at the values...but I am stuck!


Array output of XML feed:

Array
(
[SPECIALS] => Array
(
[SPECIAL] => Array
(
[0] => Array
(
[ID] => 2114
[TITLE] => COZUMEL - Allegro Cozumel ~ July 1 - August 31, 2007
[THUMBNAIL] => http://www.mysite.com/upload/file_16183.jpg
[SALESEND] => Apr 03, 2007
[ADVNIGHTS] => 5
[LOCALEID] => 322
[LOCALENAME] => Cozumel
[ADVERTISEDPRICE] => 949.00
[TRAVELDATESINFO] => July 1 - August 31, 2007
[ADDITIONALINFO] => All Inclusive ~ Kids Stay & Eat Free
)

[1] => Array
(
[ID] => 2113
[TITLE] => COZUMEL - Allegro Cozumel ~ May 1 - June 30 & September 1 - October 31, 2007
[THUMBNAIL] => http://www.mysite.com/upload/file_16183.jpg
[SALESEND] => Apr 03, 2007
[ADVNIGHTS] => 5
[LOCALEID] => 322
[LOCALENAME] => Cozumel
[ADVERTISEDPRICE] => 879.00
[TRAVELDATESINFO] => May 1 - Jun 30 & Sep 1 - Oct 31, 2007
[ADDITIONALINFO] => All Inclusive ~ Kids Stay & Eat Free
)


)

)

)




Get the file contents of the array: This is where I am having a problem???


// Get the file contents
$myar=getXmlData2($contents);
for($i=0;$i<=count($myar[TITLE]);$i++) {
// Here, we want to read the TITLE, SUMMARY, and THUMBNAIL etc. of the XML feed.



$id = $myar[ID][$i];
$title = $myar[TITLE][$i];
$thumbnaillink = $myar[THUMBNAIL][$i];
$saleends = $myar[SALESEND][$i];
$advnights = $myar[ADVNIGHTS][$i];
$localeid = $myar[LOCALEID][$i];
$localename = $myar[LOCALENAME][$i];
$adverpprice = $myar[ADVERTISEDPRICE][$i];
$pricedoubleroom = $myar[TRAVELDATESINFO][$i];
$salesend = $myar[ADDITIONALINFO][$i];
$summary = $myar[SUMMARY][$i];


//echo " <b>$id</b> $title $summary <br><br>";
}
?>



Any suggestions would be appreciated!

Thanks,

Neal

david_kw
04-02-2007, 05:53 PM
It might help if you posted the XML file as well (at least for me). But looking at this


Array
(
[SPECIALS] => Array
(
[SPECIAL] => Array
(
[0] => Array
(
[ID] => 2114
[TITLE] => COZUMEL - Allegro Cozumel ~ July 1 - August 31, 2007


you have several levels of nested arrays. I would think that would be addressed with something like

echo $myar[SPECIALS][SPECIAL][$i][ID];
echo $myar[SPECIALS][SPECIAL][$i][TITLE];

or easier would be to make


$myar_sub = $myar[SPECIALS][SPECIAL];
for ($i = 0; $i < count($myar_sub); $i++) {
echo $myar_sub[$i][ID];
echo $myar_sub[$i][TITLE];
}


david_kw