1

I'm currently experiencing issues where array_push() is not working. I have ensured the arrays are directly accessible and declared correctly. Yet I'm still receiving these warnings and the values are not being pushed onto the array.

Here is my code:

include('../connstr.inc');

$email=$_REQUEST["email"];
$datafile=$_REQUEST["datafile"];
$email_safe=preg_replace("/[^a-zA-Z]/","_",$email);
$path="../uploaded_data";
$xml = simplexml_load_file("{$path}/{$email_safe}/{$datafile}.xml");

// Retreive data details for specified activity
$lapCount = $xml->Activities->Activity->Lap->count();

// Lap Variables
$totalTime = array(); $distance = array(); $maxSpeed = array();
$calories = array(); $intensity = array(); $trigMethod = array();
$avgSpeed = array();

// Convert filename to DateTime format
$datafile = convertID($datafile);
$datafile = date('Y-m-d H:i:s', strtotime($datafile));

// Variables for accurate distance calculations
$polarDistance = true;
$lapID;
$totalLapDistance;
$firstPoint = array();
$secondPoint = array();

// Collect details for each lap
for($x = 0; $x < $lapCount; $x++) {
    $totalLapDistance = 0;

    $lapNumber = $x+1;
    $totalTime[$x] = $xml->Activities->Activity->Lap[$x]->TotalTimeSeconds;
    $distance[$x] = $xml->Activities->Activity->Lap[$x]->DistanceMeters;
    $maxSpeed[$x] = $xml->Activities->Activity->Lap[$x]->MaximumSpeed;
    $calories[$x] = $xml->Activities->Activity->Lap[$x]->Calories;
    $intensity[$x] = $xml->Activities->Activity->Lap[$x]->Intensity;
    $trigMethod[$x] = $xml->Activities->Activity->Lap[$x]->TriggerMethod;
    $avgSpeed[$x] = $xml->Activities->Activity->Lap[$x]->Extensions->LX->AvgSpeed;
    // Store activity details into the 'detail' table
    $sqlLap = "INSERT INTO lap (lapDate,lapNumber,TotalTime,distance,maxSpeed,avgSpeed,calories,intensity,trigMethod) VALUES (\"$datafile\",\"$lapNumber\",\"$totalTime[$x]\",\"$distance[$x]\",\"$maxSpeed[$x]\",\"$avgSpeed[$x]\",\"$calories[$x]\",\"$intensity[$x]\",\"$trigMethod[$x]\")";
    $runLap = mysql_query($sqlLap) or die("unable to complete INSERT action:$sql:".mysql_error());

    // Trackpoint variables
    $altitude = array(); $tDistance = array(); $latitude = array();
    $longitude = array(); $speed = array(); $pointTime = array();

    // Retreive lapID
    $lapID = getLapID();

    // Find how many tracks exist for specified lap
    $trackCount = $xml->Activities->Activity->Lap[$x]->Track->count();
    $trackpointTotalCount = 1;
    for($t = 0; $t < $trackCount; $t++) {

        // Find out how many trackpoints exist for each track
        $trackpointCount = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint->count();
        // Collect details for each specificied track point
        for($tp = 0; $tp < $trackpointCount; $tp++) {
            $altitude[$tp] = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint[$tp]->AltitudeMeters;
            $tDistance[$tp] = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint[$tp]->DistanceMeters;
            $pointTime[$tp] = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint[$tp]->Time;
            $latitude[$tp] = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint[$tp]->Position->LatitudeDegrees;
            $longitude[$tp] = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint[$tp]->Position->LongitudeDegrees;
            $speed[$tp] = $xml->Activities->Activity->Lap[$x]->Track[$t]->Trackpoint[$tp]->Extensions->TPX->Speed;

            // Check Track point
            if(checkTP($altitude[$tp], $tDistance[$tp], $latitude[$tp], $longitude[$tp], $speed[$tp])) {

                // Check if accurate distance should be calculated
                if($polarDistance) {
                    $aa = $latitude[$tp];
                    $bb = $longitude[$tp];
                    $cc = $altitude[$tp];
                    if($tp == 0) {
                        array_push($firstPoint, $aa, $bb, $cc);
                    } else if($tp != 0) {
                        array_push($secondPoint, $aa, $bb, $cc);
                    }
                    printArray($firstPoint);
                    printArray($secondPoint);
                    // Add distance between trackpoints to total lap distance
                    $totalLapDistance += calcDistance($firstPoint, $secondPoint);
                }

                // Insert current trackpoint data into 'trackpoint' table
                $sqlTC = "INSERT INTO trackpoint (tpDate,tpNumber,altitude,distance,latitude,longitude,speed,pointTime) VALUES (\"$datafile\",\"$trackpointTotalCount\",\"$altitude[$tp]\",\"$tDistance[$tp]\",\"$latitude[$tp]\",\"$longitude[$tp]\",\"$speed[$tp]\",\"$pointTime[$tp]\")";
                $runTC = mysql_query($sqlTC) or die("unable to complete INSERT action:$sql:".mysql_error());
            }

            $trackpointTotalCount++;

            if($polarDistance) {
                if($tp != 0) {
                    unset($firstPoint);
                    $firstPoint = &$secondPoint;
                    unset($secondPoint);
                }
            }
        }
    }
    if($polarDistance) {
        if($tp != 0) {
            // Update lap with more accurate distance
            echo $totalLapDistance . '<br />';
            $sqlUlap = "UPDATE lap SET accDistance='$totalLapDistance' WHERE lapID = '$lapID' ";
            $runUlap = mysql_query($sqlUlap) or die("unable to complete UPDATE action:$sql:".mysql_error());
        }
    }
}

I didn't include all of the code below as there is quite a lot and I very much doubt it's relevant.

The warnings themselves only appear when trying to push a variable onto $secondPoint:

array_push($secondPoint, $aa, $bb, $cc);

However values are not being pushed onto either of the variables ($firstPoint, $secondPoint)

As a test I did echo $aa,bb and $cc and they did contain correct values.

Anybody have an idea of what I'm doing wrong?

EDIT: I have showed more of the code as I do use these arrays later, however this should not affect how the values are initially pushed? Below is some code which may affect it, namely the assign by reference?

if($polarDistance) {
                if($tp != 0) {
                    unset($firstPoint);
                    $firstPoint = &$secondPoint;
                    unset($secondPoint);
                }
            }
7
  • tbh I'd just use $secondPoint[] = $aa; ...etc. Commented Apr 23, 2014 at 22:24
  • $lapID; $totalLapDistance; this does NOT work in PHP. You have to set a value. ( Just set null ). Maybe this is the error, and he isnt declaring the variables afterwards anymore. Normally this should give a fatal error so the other code isnt even used.. Commented Apr 23, 2014 at 22:25
  • 1
    The problem is with $secondPoint - when you get to that array_push, it's not an array. I don't see any reason why that would be, but your code is incomplete and you may be overwriting $secondPoint in the code that's not shown. Commented Apr 23, 2014 at 22:26
  • The additional if-clause else if($tp != 0) after if($tp == 0) is odd, just leave it out. Commented Apr 23, 2014 at 22:26
  • @Xatenev it will evaluate without errors, until you try to access it before setting a value to it. $totalLapDistance = 0 at the beginning of the loop take cares of one, $lapID = getLapID(); takes care of the other, so no errors/warnings here. Commented Apr 23, 2014 at 22:27

1 Answer 1

2

That unset($secondPoint) will probably do it.

Try this instead:

if($polarDistance) {
               if($tp != 0) {
                    $firstPoint = $secondPoint;

                    $secondPoint = array();
                }
            }
Sign up to request clarification or add additional context in comments.

1 Comment

This worked! I'm still not sure how this affected the results on the first cycle of the loop. However, I'm nt going to complain. Been stuck on this for a while; Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.