JSON - XML解码问题

时间:2015-03-15 15:08:43

标签: php xml json web-services curl

您好我正在尝试根据CURL XML的响应创建一个简单的列表。我看到的问题是,当我转换yo JSON时,使用json_decode,我有一些数组有2或3个elemtns,有时只有1,什么时候只有一个我有错误信息,我不知道如何验证这个问题。 这是我的PHP脚本:

enter code here
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($output);
$json = json_encode($xml);
$arr = json_decode($json,true);

$hoteles = count($arr['ServiceHotel']);

for ($x = 0; $x <= $hoteles; $x++) {
     $hotel = $arr['ServiceHotel'][$x]['HotelInfo']['Name'] ;
     $room_count =  count($arr['ServiceHotel'][$x]['AvailableRoom'][0]['HotelRoom']['RoomType']); 
for ($y = 0; $y <= $room_count; $y++) {
    if($room_count == 0){
        $room_type  = $arr['ServiceHotel'][$x]['AvailableRoom']['HotelRoom']['RoomType'] ;
        $board      = $arr['ServiceHotel'][$x]['AvailableRoom']['HotelRoom']['Board'] ;
        $rate       = $arr['ServiceHotel'][$x]['AvailableRoom']['HotelRoom']['Price']['Amount'] ;
    }else{
        $room_type  = $arr['ServiceHotel'][$x]['AvailableRoom'][$y]['HotelRoom']['RoomType'] ;
        $board      = $arr['ServiceHotel'][$x]['AvailableRoom'][$y]['HotelRoom']['Board'] ;
        $rate       = $arr['ServiceHotel'][$x]['AvailableRoom'][$y]['HotelRoom']['Price']['Amount'] ;   
    }    
print "[$x]Hotel :  $hotel <br> Room Type : $room_type > $board > $rate <hr> ";
}

这是我想要得到的结果:

Hoteles: 19 
Rooms: 1 
[0]Hotel : Posada Los Arcos Holbox 
Room Type : DOUBLE ECONOMY > ROOM ONLY > 800.000
[0]Hotel : Posada Los Arcos Holbox 
Room Type : DOUBLE STANDARD > ROOM ONLY > 1000.000
Rooms: 1 
[1]Hotel : Hacienda de Castilla 
Room Type : DOUBLE STANDARD > ROOM ONLY > 838.500
[1]Hotel : Hacienda de Castilla 
Room Type : DOUBLE STANDARD > AMERICAN BREAKFAST > 1126.740
Rooms: 1 
[2]Hotel : Hacienda de Castilla 
Room Type : DOUBLE STANDARD > ROOM ONLY > 890.900
[2]Hotel : Hacienda de Castilla 
Room Type : DOUBLE STANDARD > AMERICAN BREAKFAST > 1179.140

但是当[AvailableRoom]数组只有一个项目时,我收到此错误: 注意:未定义的偏移量:C:\ wamp \ www

中为0

提前谢谢你,任何帮助!!

0 个答案:

没有答案