Foreach/if addition and subtraction
<p>I've got a foreach loop in PHP that chucks out numbers from a GPX file and produces a table.</p>

<p>For example:</p>

<pre><code>Distance | Elevation
--------------------
0        | 268
0.27     | 294
0.87     | 294
1.14     | 321
1.25     | 324
1.49     | 371
2.30     | 399
3.00     | 372
3.91     | 346
</code></pre>

<p>I want to add up when the elevation gets higher. So, for this table:</p>

<p>Increase: 294 - 268 = 26, 294 - 294 = 0 (26), 321 - 294 = 27 (53), 324 - 321 = 3 (56)..... and so on</p>

<p>I also want to count when it decreases and keep it separate to get total increase and total decrease in elevation.</p>

<p>So, I'm basically wanting to ask if each previous number is higher or lower and then, depending on that, add it to a variable for use later.</p>

<p>Any ideas?</p>

<p><strong>I tried to keep it simple but code was asked for... apologies in advance!</strong></p>

<pre><code>&lt;?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index =&gt; $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

//$xmlUrl = "../wp-content/uploads/2012/11/stoodley-pike-walk-cragg-vale-withens-clough-circular.gpx"; // XML feed file/URL
$uploadyr = $_GET['yr'];
$uploadmth = $_GET['mth'];
$gpx = $_GET['gpx'];

$xmlUrl = "../wp-content/uploads/" . $uploadyr . "/" . $uploadmth . "/" . $gpx . ".gpx";
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
?&gt;

&lt;!doctype html&gt;
&lt;head&gt;
    &lt;title&gt;Graph&lt;/title&gt;

    &lt;!--&lt;link href="visualize.css" rel="stylesheet"&gt;
    &lt;link href="visualize-light.css" rel="stylesheet"&gt;--&gt;
    &lt;link href="visualize-override.css" rel="stylesheet"&gt;

    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;    
    &lt;script src="visualize.jQuery.js"&gt;&lt;/script&gt;
    &lt;script&gt;
        $(document).ready(function(){
            $('#elevation').visualize({
                type: 'area',
                parseDirection: 'y',
                colors: ['#339933','#00ff00','#0000ff','#ffff00','#ff00ff','#00ffff','#000000','#666666','#AAAAAA']
            });
            $('.visualize-labels-x li:odd').hide();
        });
    &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;div id="wrapper"&gt;

&lt;?php

echo '&lt;table id="elevation"&gt;&lt;caption&gt;Elevation Profile&lt;/caption&gt;&lt;thead&gt;&lt;td&gt;Total Distance (mi)&lt;/td&gt;&lt;th&gt;Elevation&lt;/th&gt;&lt;/thead&gt;&lt;tbody&gt;';

foreach($arrXml["rte"]["rtept"] as $single){

        $lat1 = $prevLat;
        $lng1 = $prevLon;
        if($prevLat == null &amp;&amp; $prevLon == null){
            $lat1 = $single["@attributes"]["lat"];
            $lng1 = $single["@attributes"]["lon"];
        }
        $lat2 = $single["@attributes"]["lat"];
        $lng2 = $single["@attributes"]["lon"];

        $pi80 = M_PI / 180;
        $lat1 *= $pi80;
        $lng1 *= $pi80;
        $lat2 *= $pi80;
        $lng2 *= $pi80;

        $r = 6372.797; // mean radius of Earth in km
        $dlat = $lat2 - $lat1;
        $dlng = $lng2 - $lng1;
        $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $km = $r * $c;

        $distance += $km;

        $buildrow = '';
        $buildrow .= '&lt;tr&gt;';     
        if($prevLat !== null &amp;&amp; $prevLon !== null){
            $buildrow .= '&lt;th&gt;' . number_format(($miles ? ($distance * 0.621371192) : $distance),2) . '&lt;/th&gt;';
        } else {
            $buildrow .= '&lt;th&gt;0&lt;/th&gt;';
        }
        $buildrow .= '&lt;td&gt;' . number_format($single["ele"],0) . '&lt;/td&gt;';
        $buildrow .= '&lt;/tr&gt;';

        echo $buildrow;

    $prevLat = $single["@attributes"]["lat"];
    $prevLon = $single["@attributes"]["lon"];
}
echo '&lt;/tbody&gt;&lt;/table&gt;';

echo '&lt;p&gt;Total Distance: ' . number_format($distance,2) . 'mi&lt;/p&gt;';

?&gt;

&lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;
</code></pre>
