1
我想根據給定的信息從PHP數組中提取信息。如何從php數組中提取數據如果給定了相同數組中的某個值
PHP代碼
$xmlstring = file_get_contents('file.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]
能否請您協助。
現在我得到:從線「的foreach($ '警告爲的foreach()提供的參數無效'酒店爲$ hotel){' –
我沒有看到任何地方定義的$酒店。 – Sherlock