是否可以对数组中的项使用if-iset?
示例:
if (isset($cityWeatherRssUrls[0])
这样做时,我收到以下警告。
警告:get_temperature()[function.get-temperature]:节点不再存在于第51行上的C:\examplep\htdocs\Twinz\includes\getWeather.php中
第51行开始于我的getWeather.php脚本中函数get_temperature(SimpleXMLElement$xml)的//Pull temperature注释之后。
以下包含的所有相关脚本和功能
cityConfig.php-用于向yahoo的天气XML声明所有城市和相应的URL。
// Define arrays //
$cities = array();
$cityWeatherRssUrls = array();
// Feed URL's //
$yahooWeather = 'http://weather.yahooapis.com/forecastrss?p=';
// City 1 //
$city = 'London';
$cityWeatherRssUrl = $yahooWeather . $cityWeatherFeedCode . '&u=c';
array_push($cities, $city);
array_push($cityWeatherRssUrls, $cityWeatherRssUrl);
getWeather.php-包含解析XML标记内容和显示内容的函数
function get_current_weather($weatherRssUrl) {
// Get XML data from source
if (isset($weatherRssUrl)) {
$feed = $weatherRssUrl;
} else {
echo 'Feed not found. Check URL';
}
checkFeedExists($feed);
$xml = new SimpleXmlElement($feed);
$weather = get_temperature($xml);
return $weather;
}
function get_temperature(SimpleXMLElement $xml) {
// Pull temperature from XML
$weather['temp'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->temp;
echo round($weather['temp']) . "°C" . "<br />";
return $temp;
}
//Display City Content //
function displayCityContent($cityWeatherRssUrls, $columnSubheading) {
if (isset($cityWeatherRssUrls)) {
echo get_current_weather($cityWeatherRssUrls);
echo $columnSubheading;
echo get_forecast_weather($cityWeatherRssUrls);
echo '<br />';
} else {
content_unavailable();
}
}
threeColumnContainer.php-用于在html列中显示天气内容
<ul class="columns">
<li class="col1">
<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
<p>
<?php
if (isset($cityWeatherRssUrls[0]) && ($currentPage == 1)) {
echo displayCityContent($cityWeatherRssUrls[0], $columnSubheading);
我可以确认$cityWeatherRssUrls[0]包含伦敦的天气url,因为如果我递增数组@cityWeatherRssUrls[1],我会收到一条消息,说找不到提要。
有什么想法吗?
提前感谢
您的代码中缺少了一些关键概念,但希望以下内容能帮助您修复代码。。。
<?php
function get_feed($feed_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rss = curl_exec($ch);
curl_close($ch);
return $rss;
}
function get_weather_feed_xml($feed_url = null)
{
$feed = get_feed($feed_url);
return new SimpleXmlElement($feed);
}
function get_temperature($feed_url)
{
if(stripos($feed_url, 'yahooapis') !== false)
{
return get_temperature_yahoo($feed_url);
}
return false;
}
function get_temperature_yahoo($feed_url)
{
if(($xml = get_weather_feed_xml($feed_url)) !== false)
{
return (float)$xml->channel->item->children('yweather', true)->condition->attributes()->temp;
}
return false;
}
function get_city_temperature($city_name, $city_WOEID, $feeds)
{
$results = array();
foreach($feeds as $feed_name => $feed_url)
{
$feed_url = str_replace(array('{WOEID}', '{NAME}'), array($city_WOEID, $city_name), $feed_url);
$results[$feed_name] = get_temperature($feed_url);
}
return $results;
}
function get_cities_temperature($cities, $feeds)
{
$results = array();
foreach($cities as $city_name => $city_WOEID)
{
$results[$city_name] = get_city_temperature($city_name, $city_WOEID, $feeds);
}
return $results;
}
// -------------------------------
$cities = array(
'London' => 44418,
'Los Angeles' => 2442047,
'Sydney' => 1105779,
);
$feeds = array(
'Yahoo' => 'http://weather.yahooapis.com/forecastrss?u=c&w={WOEID}',
);
$check = get_cities_temperature($cities, $feeds);
echo '<pre>';
print_r($check);
echo '</pre>';