Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:23:24+00:00 2026-06-15T19:23:24+00:00

I’ve got a foreach loop in PHP that chucks out numbers from a GPX

  • 0

I’ve got a foreach loop in PHP that chucks out numbers from a GPX file and produces a table.

For example:

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

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

Increase: 294 – 268 = 26, 294 – 294 = 0 (26), 321 – 294 = 27 (53), 324 – 321 = 3 (56)….. and so on

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

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.

Any ideas?

I tried to keep it simple but code was asked for… apologies in advance!

<?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 => $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);
?>

<!doctype html>
<head>
    <title>Graph</title>

    <!--<link href="visualize.css" rel="stylesheet">
    <link href="visualize-light.css" rel="stylesheet">-->
    <link href="visualize-override.css" rel="stylesheet">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>    
    <script src="visualize.jQuery.js"></script>
    <script>
        $(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();
        });
    </script>
</head>

<body>

<div id="wrapper">

<?php

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

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

        $lat1 = $prevLat;
        $lng1 = $prevLon;
        if($prevLat == null && $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 .= '<tr>';     
        if($prevLat !== null && $prevLon !== null){
            $buildrow .= '<th>' . number_format(($miles ? ($distance * 0.621371192) : $distance),2) . '</th>';
        } else {
            $buildrow .= '<th>0</th>';
        }
        $buildrow .= '<td>' . number_format($single["ele"],0) . '</td>';
        $buildrow .= '</tr>';

        echo $buildrow;

    $prevLat = $single["@attributes"]["lat"];
    $prevLon = $single["@attributes"]["lon"];
}
echo '</tbody></table>';

echo '<p>Total Distance: ' . number_format($distance,2) . 'mi</p>';

?>

</div>

</body>

</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:23:26+00:00Added an answer on June 15, 2026 at 7:23 pm

    You just simply need to add a variable before the loop to hold the previous value. Say $prevValue. Then in the loop, at the end of the loop, you set it to the current value. When the loop runs the next one, it’ll be the previous value.

    Then, just check if it’s more or less then the current value.

    if($val > $prevVal){}
    elseif($val < $prevVal){}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.