PHP - array_push()期望参数1为数组,在给定的情况下为null

时间:2014-04-23 22:21:39

标签: php arrays

我目前遇到的问题是array_push()无效。我确保数组可以直接访问并正确声明。然而,我仍然收到这些警告,并且这些值没有被推到阵列上。

这是我的代码:

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());
        }
    }
}

我没有包含下面的所有代码,因为有很多代码,我非常怀疑它是否相关。

警告本身仅在尝试将变量推送到$ secondPoint时出现:

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

但是,值不会被推送到任何一个变量($ firstPoint,$ secondPoint)

作为测试,我确实回显了$ aa,bb和$ cc,它们确实包含了正确的值。

任何人都知道我做错了什么?

编辑:我已经展示了更多代码,因为我稍后会使用这些数组,但是这不应该影响最初推送值的方式?下面是一些可能影响它的代码,即按引用分配?

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

1 个答案:

答案 0 :(得分:2)

unset($secondPoint)可能会这样做。

请改为尝试:

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

                    $secondPoint = array();
                }
            }