我正在使用Met Office RSS Feed:
$metourl = "http://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/UK";
$metoxml = simplexml_load_file($metourl);
$count = $metoxml->channel->item;
我可以很容易地确定是否有任何“天气警告”(在这种情况下):
if($count && $count->count() >= 1){
如果可能,我想要做的是计算在
下发生'YELLOW'
或'RED'
次警告的次数
$metoxml->channel->item->warningLevel
所以我可以回应那个?
E.g。 "There are x yellow and x red warnings."
。
谢谢!
答案 0 :(得分:0)
您可以使用xpath
方法:
$metourl = "http://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/UK";
$metoxml = simplexml_load_file($metourl);
$metoxml->registerXpathNamespace('metadata',
'http://metoffice.gov.uk/nswws/module/metadata/1.0');
$wl = $metoxml->xpath('//channel/item/metadata:warningLevel');
$counters = [ 'YELLOW' => 0, 'RED' => 0 ];
foreach ($wl as $e) {
$str = trim((string)$e);
if ($str === 'YELLOW')
$counters['YELLOW']++;
elseif ($str === 'RED')
$counters['RED']++;
}
printf('There are %d yellow and %d red warnings.',
$counters['YELLOW'], $counters['RED']);
示例输出
There are 14 yellow and 0 red warnings.