xml和php for循环在另一个for循环中

时间:2012-03-22 18:28:40

标签: php xml api foreach

我有两个用expedia api更新的xml文件。我试图从一个获取房间信息和房间代码,并在另一个xml文件中找到该房间代码以检索necesarry信息。但我似乎无法弄清楚如何做到这一点。这是xml架构:

XML 1(hotel_request.xml):

<RoomTypes size="3">
<RoomType roomCode="9995" roomTypeId="157240">
<description>Standard</description>
</RoomType>
</RoomTypes>

XML 2(room_request.xml)

<rateCode>9995</rateCode>
<roomTypeCode>9995</roomTypeCode>
<rateDescription>Standard - Super Saver- No meals included</rateDescription>
<roomTypeDescription>Standard - Super Saver- No meals included</roomTypeDescription>

我能够成功连接到每个文件,但是我从第一个xml文件中得到了很多结果。所以我不得不使用foreach循环来完成它并单独回应每个房间:

PHP:

//Getting xml file and data

    $data = simplexml_load_file('hotel_request.xml'); 
    $info = $data->HotelSummary;
    //foreach loop to go through and echo out all rooms
    foreach($data->RoomTypes as $rooms):
        $roomCode = $rooms->RoomType['roomCode'];
        $roomName = $rooms->RoomType->description;
    endforeach;

//Now I need to go through this above loop and get the roomCode, then look for that roomCode in the second xml file and get all it's associated information. So this is what I tried which DID NOT work:
foreach($data->RoomTypes as $rooms):
    $roomCode = $rooms->RoomType['roomCode'];
    $roomName = $rooms->RoomType->description;

//Here is where I added in other file
$avail = simplexml_load_file('room_request.xml');

    foreach($avail->HotelRoomResponse->$roomCode as $rooms):
    $promo = $rooms->RateInfos->RateInfo->ChargeableRateInfo->NightlyRatesPerRoom->NightlyRate['promo'];

endforeach;
endforeach;

请希望有人可以帮助我。感谢!!!

0 个答案:

没有答案