I'm trying to make a simple script that visualizes runtastic data but I'm having a little problem. I run the following script, which only displays the latest activity.
$myRuntasticActivities = $runtastic->getActivities(null, null, 2017);
$distanceKM = $myRuntasticActivities[0]->distance / 1000;
echo "In 2017 I ran $distanceKM KM";
Result output: In 2017 I ran 6,8KM.
If I change the [0] to [1] I get the second most recent activity. How can I add all results up? I believe the runtastic script is limited to showing the latest 419 results. Is there a way to add 0 -> 419?
I've tried array_sum, but I can't get it to work.
You can use array_reduce
.
$sum = array_reduce($myRuntasticActivities, function($carry, $item){
return $carry + $item->distance;
}, 0) / 1000;
You could use a foreach
.
$sum = 0;
foreach ($myRuntasticActivities as $act) {
$sum += $act->distance / 1000;
}
You could use a simple for
.
$sum = 0;
for ($i = 0; $i < count($myRuntasticActivities); $i++) {
$sum += $myRuntasticActivities[$i]->distance / 1000;
}