Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP导入外部XML文件,求和值_Php_Xml - Fatal编程技术网

使用PHP导入外部XML文件,求和值

使用PHP导入外部XML文件,求和值,php,xml,Php,Xml,正如标题所说,我正在将一个外部XML文件导入一个站点。这实际上是来自世界各地观测站的天气数据。我可以解析和显示数据,没有问题。我的问题是我试图总结一组特定的数据 以下是基本代码: <?php $xml = simplexml_load_file('https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&

正如标题所说,我正在将一个外部XML文件导入一个站点。这实际上是来自世界各地观测站的天气数据。我可以解析和显示数据,没有问题。我的问题是我试图总结一组特定的数据

以下是基本代码:

<?php
$xml = simplexml_load_file('https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=kbwi&hoursBeforeNow=65');

for($i=0;$i<=60;$i++)
{
$precip[$i] = $xml->data->METAR[$i]->precip_in;
echo $precip[$i];
}    
?>
但我想做的一件事是24小时的总降雨量。观察结果并不总是定期或每小时更新一次;意思是如果我要做这样的事情:

$rainTotal = $precip[0]+$precip[1]+$precip[2]...+$precip[23];
它不一定会给我24小时的总降雨量。因此,我需要一种方法来计算'precip_in'或$precip中包含的所有降雨量值

我该怎么做有什么想法吗

编辑:基于以下评论的一些澄清:

回声和var_dump-ing$precip[$i]工作正常。但如果我尝试以下方法:

<?php
 $xml = simplexml_load_file('https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=kbwi&hoursBeforeNow=65');

for($i=0;$i<=60;$i++)
{
$precip[$i] = $xml->data->METAR[$i]->precip_in;
echo array_sum($precip[$i]);
}    
?>
METAR[$i]>precip\u in;
回波阵列_和($precip[$i]);
}    
?>
我得到一张空白页。对array_sum($precip[$i])执行var_转储会导致一行中多次出现“NULL”


我尝试将XML字符串转换为浮点或字符串,但得到了相同的结果。

值$XML->data->METAR[$I]->precip_in是一个对象((SimpleXMLElement)#12(1){[0]=>string(5)“0.005”})。它不是数值,所以没有数值。但是,您可以将其转换为浮点,并获得您期望的数值

for($i=0;$i<=60;$i++)
{
  $precip[$i] = (float)$xml->data->METAR[$i]->precip_in;
  echo $precip[$i];
}  
for($i=0;$idata->METAR[$i]->precip\u in;
echo$precip[$i];
}  

这些浮点值可以求和。

考虑XPath数组返回的
数组\u sum

$xml = simplexml_load_file('https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=kbwi&hoursBeforeNow=65');

$result = array_sum(array_map("floatval", $xml->xpath('//METAR/precip_in')));

echo $result;
// 10.065
或者,您可以使用XPath的
sum()
,这需要使用DOMDocument而不是SimpleXML,具体来说:


您是说
echo$precip[$i];
输出正确的值,但是
var\u dump($precip);
返回
null
?“通过使用数组求和,我得到一个空白页”您能否显示生成此消息的代码以及apache/php日志中可能出现的相关错误消息?感谢您的回复。是的,我尝试将其转换为浮点、字符串等形式。当我这样做时,我仍然能够回显$precip[$I],但我仍然无法实现$precip[$I]的数组和。如果我对数组求和($precip[$I])进行var_转储,我会得到一排“NULL”。@Steppin_Razor$precip[$I]不是数组,它是数组中索引$I处的一个元素。如果在循环后进行数组求和($precip),会得到什么?
$xml = simplexml_load_file('https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=kbwi&hoursBeforeNow=65');

$result = array_sum(array_map("floatval", $xml->xpath('//METAR/precip_in')));

echo $result;
// 10.065
$dom = new DOMDocument;
$dom->load('https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=kbwi&hoursBeforeNow=65');

$xpath = new DOMXPath($dom);
$sum = (float)$xpath->evaluate('sum(//METAR/precip_in)');

echo $sum;
// 10.065