Jump to content

array_sum returns 0


oeniir

Recommended Posts

Hi,

A newbie question. I am trying to calculate average exchange rates from the daily rates extracted from an XML file. I store all the extracted rates into an array, but when I try to add them up using array_sum, I get 0 as a result. Any idea what might be wrong? I also tried adding up the array results "manually", but it seems that doing that ignores all the decimal numbers.

Here's the code (I've been testing various things, so it has some weird statements):

[code]$xmlsource=simplexml_load_file('http://www.catoss.com/labs/eurofxref-hist-90d.xml');
print "<html><head><title>Currency Converter</title></head><body><h1>Current Exchange Rates</h1>";
$ratesArray;

foreach ($xmlsource->Cube->Cube as $dates) {
foreach ($dates->Cube as $currencies) {
if ($currencies['currency'] == "SEK") {
//print substr($dates['time'], 0, 4) . " - ";
//print $currencies['currency'] . ": ";
$ratesArray[] = $currencies['rate'];
//print $currencies['rate'] . "<br>";
} else {
// nothing here yet
}
}
}
$test = $ratesArray[0]+$ratesArray[1];
print $test . "<br>";
print array_sum($ratesArray) . "<br>";
print count($ratesArray) . "<br>";
$avgExchangeRate = $total/count($ratesArray);
print "The average exchange rate for the past 90 days is $avgExchangeRate";
print "</body></html>";
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/30973-array_sum-returns-0/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.