要检查每个foreach()
语句是否适用于数组,每个语句中的第一个参数是(array)
。尽管如此,下面的代码并没有返回任何数据。
该脚本旨在根据通过$airports
变量提供的数据从FAA的XML机场状态JSP文件(http://www.fly.faa.gov/flyfaa/xmlAirportStatus.jsp)中提取适用的数据。
删除每个(array)
语句中的foreach()
参数后,脚本将返回适用的数据,但也会返回以下警告:
Warning: Invalid argument supplied for foreach()
代码:
$xml = simplexml_load_file("http://www.fly.faa.gov/flyfaa/xmlAirportStatus.jsp");
$airports = array('ATL','AUS','BOS','DEN','LAS','LGA','LGB','JFK','OAK','PDX','PHX','RNO','SAN','SAT','SEA','SFO','SJC','SLC','SMF');
$effected = array();
$count = 0;
foreach((array) $xml->Delay_type as $Delay) {
foreach((array) $Delay->Ground_Stop_List->Program as $Program) {
if(in_array($Program->ARPT, $airports)) {
$effected[] = $Program->ARPT;
if($count == 0) {
echo '<div class="alert">';
}
echo 'Departure traffic destined to <b>'.$Program->ARPT.'</b> is being held at its origin due to <b>'.$Program->Reason.'</b> until <b>'.$Program->End_Time.'</b><br />';
$count++;
}
}
foreach((array) $Delay->Ground_Delay_List->Ground_Delay as $Ground_Delay) {
if(in_array($Ground_Delay->ARPT, $airports)) {
$effected[] = $Ground_Delay->ARPT;
if($count == 0) {
echo '<div class="alert">';
}
/* effected[] = $Ground_Delay->ARPT; */
echo 'Departure traffic destined to <b>'.$Ground_Delay->ARPT.'</b> is facing an average delay of <b>'.$Ground_Delay->Avg.'</b> due to <b>'.$Ground_Delay->Reason.'</b><br />';
$count++;
}
}
}