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

Like the title says, I'm importing an external XML file into a site. It's actually weather data from observation sites around the world. I can parse and display the data no problem. My problem is I'm trying to sum up a specific set of data.

Here's the basic code:

<?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];
}    
?>

This will echo all the values from 'precip_in' from the XML file, and it does work. But if I try to sum up the data in $precip or in 'precip_in' by using array_sum, I get a blank page. Using var_dump returns "NULL" a bunch of times.

Now, I could manually sum the values by doing something like:

$rainTotal = $precip[0]+$precip[1]+$precip[2];

But one thing I want to do with this is a 24 hour rainfall total. The observations aren't always updated at regular or hourly intervals; meaning that if I were to do something like this:

$rainTotal = $precip[0]+$precip[1]+$precip[2]...+$precip[23];

It would not necessarily give me the 24 hour rain total. So, I need a way to sum all the rainfall values contained within 'precip_in' or $precip.

Any ideas on how I should proceed?

EDIT: Some clarifications based on the comments below:

Echoing and var_dump-ing $precip[$i] work fine. But if I try the following:

<?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]);
}    
?>

I get a blank page. Doing a var_dump of array_sum($precip[$i]) results in "NULL" a bunch of times in a row.

I have tried casting the XML string as either a float or a string, but I get the same results.

the value of $xml->data->METAR[$i]->precip_in is an object ((SimpleXMLElement)#12 (1) { [0]=> string(5) "0.005" }). It's not numeric, so it has no numeric value. You can cast this to a float however and get the numeric value your were expecting.

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

These float values can be summed.

Consider an array_sum on an XPath array return:

$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

Alternatively you can use XPath's sum() which requires using DOMDocument and not SimpleXML, specifically DOMXPath::evaluate:

$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