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