My php script is getting data from an xml file. I am fetching a list of prices and echoing them in a foreach statement. The problem is I need to calculate the sum of prices outside the foreach. Here is what I've done so far. Converting to array then going array_sum is the only thing I can think of.
<?php
$data = simplexml_load_file("products.xml");
foreach ($data as $val) {
echo $val->price . "<br>";
$total[] = $val->price;
}
$sum = array_sum($total);
echo $sum;
So far, this is not returning the total. It simply returns 0, which is weird because when I vardump $total, it shows up as an array.
$val->price;to an int.it shows up as an array.but if it is an empty array, than you will get 0