我想根据给定的信息从PHP数组中提取信息。
Php是
$xmlstring = file_get_contents('http://www.bookingassist.ro/XML/test.xml');
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$HotelCodes[] = array('BG01I9', 'BG53I4', 'BG23I7');
$code = $HotelCodes[1];
if (!$code) {
throw new Exception("No Hotel Code specified");
}
foreach ($HotelCodes as $code) {
foreach ($hotels as $hotel) {
if (strcasecmp($hotel['HotelCode'], $code) === 0) {
echo "{$hotel['Latitude']}:{$hotel['Longitude']}<br/>";
foreach ($hotel['HotelImages']['ImageUrl'] as $img) {
echo "<img src='{$img}'/><hr/>";
}
break;
}
}
}
带有一条记录的 print_r $array
(数组有多条记录):
Array
(
[Hotel] => Array
(
[0] => Array (
[HotelCode] => BG01I9
[Latitude] => 42.6039
[Longitude] => 23.3954
[HotelImages] => Array (
[ImageURL] => Array (
[0] => http://image.metglobal.com/hotelimages/BG01I9/6481077_0x0.jpg
[1] => http://image.metglobal.com/hotelimages/BG01I9/6481092_0x0.jpg
[2] => http://image.metglobal.com/hotelimages/BG01I9/6481109_0x0.jpg
[3] => http://image.metglobal.com/hotelimages/BG01I9/6481139_0x0.jpg
[4] => http://image.metglobal.com/hotelimages/BG01I9/6481163_0x0.jpg
[5] => http://image.metglobal.com/hotelimages/BG01I9/6480990_0x0.jpg
[6] => http://image.metglobal.com/hotelimages/BG01I9/6481002_0x0.jpg
[7] => http://image.metglobal.com/hotelimages/BG01I9/6481015_0x0.jpg
[8] => http://image.metglobal.com/hotelimages/BG01I9/6481033_0x0.jpg
[9] => http://image.metglobal.com/hotelimages/BG01I9/6481058_0x0.jpg
)
)
)
)
我希望根据我提供的HotelCodes回应酒店图片,纬度和经度:
酒店代码为$HotelCodes[0]
收到的错误是:
Fatal error: Uncaught exception 'Exception' with message 'No Hotel Code specified' in /home/truckass/public_html/site/test/teste.php:10 Stack trace: #0 {main} thrown in /home/truckass/public_html/siteo/test/teste.php on line 10
我需要回复HotelCodes[1]
$img[1][0] ....$img[1][5]
$Latitude[1]
$Longitude[1]
请你帮忙。
答案 0 :(得分:0)
循环数组并检查HotelCode
是否与指定匹配:
$HotelCodes = array('BG01I9', 'BG53I4', 'BG23I7');
if (!$code) {
throw new Exception("No Hotel Code specified");
}
foreach ($HotelCodes as $code) {
foreach ($hotels as $hotel) {
if (strcasecmp($hotel['HotelCode'], $code) === 0) {
echo "{$hotel['Latitude']}:{$hotel['Longitude']}<br/>";
foreach ($hotel['HotelImages']['ImageUrl'] as $img) {
echo "<img src='{$img}'/><hr/>";
}
break;
}
}
}