我正在从Yahoo Weather检索数据,并且能够显示到目前为止我需要的元素。我很难理解如何使用条件代码(我想我需要将数字转换为整数)来拥有一个字符串,以便我可以显示自己的自定义图标。由于我跟踪的地区天气相当温和,我想只分配少量图标,但每个图标都包含一系列条件代码。到目前为止,这是我的代码 -
function bm_getWeather($ code ='',$ temp ='c'){
$file = 'http://weather.yahooapis.com/forecastrss?w=' . $code . '&u=' . $temp;
$request = new WP_Http;
$result = $request->request($file);
if (isset($result->errors)) {
return FALSE;
}
$data = $result['body'];
$output = array (
'temperature' => bm_getWeatherProperties('temp', $data),
'weather_code' => bm_getWeatherProperties('code', $data),
'class' => bm_getWeatherProperties('code', $data),
'weather' => bm_getWeatherProperties('text', $data),
);
return $output;
}
function weather_icon(){
$data = bm_getWeather($code = '', $temp = 'c');
// Error is here
$nums = (int)$data['weather_code']; // Needs to be cast as an integer
$severe = array(12, 13, 14, 16, 19);
$rain = array(3200);
// Therefore the maths is wrong
switch($nums) {
case (in_array($nums, $severe)):
$cat = 'severe';
break;
case (in_array($nums,$rain)):
$cat = 'snow';
break;
default:
$cat = 'happy';
break;
}
return $cat;
}
function bm_getWeatherProperties($ needle,$ data){
$regex = '<yweather:condition.*' . $needle . '="(.*?)".*/>';
preg_match($regex, $data, $matches);
return (string)$matches[1];
}
任何帮助都会非常感激,因为我的PHP技能现在还不够。