2016-09-25 45 views
1

我可能找到一種方法來解決我遇到的這個問題,但真的想了解什麼語言構造造成它。構建一個數組給PHP未定義的偏移:0

邏輯如下,我想建立一個會話變量,因爲用戶點擊Google地圖上的一個路線點陣列。該POST分組是JSON對象:

?waypoint={"waypoint":[{"lat":"23.3","long":"145"}]} 

接受POST和解釋的代碼是:

<?php 
    session_start(); 
    //session_destroy(); 
    //die; 
    //$waypoints[] = array(); <<== uncomment this and the behaviour changes 
    if (isset ($_GET ['waypoint'])) { 
     $waypoint = $_GET ['waypoint']; 
     $waypoint = json_decode($waypoint,true); 
     print_r($waypoint['waypoint']); <<<========output 1 
     if (isset ($_SESSION ['waypoints'])) { 
      $waypoints = $_SESSION ['waypoints']; 
      $waypoints [] = $waypoint['waypoint']; 
      $_SESSION ['waypoints']=$waypoints; 
     } else { 
      $_SESSION ['waypoints'] = $waypoint['waypoint']; 
      $waypoints[] = $waypoint['waypoint']; 
     } 
    } 

    foreach($_SESSION ['waypoints'] as $var) { 
     var_dump($var); <<<<====== Output 2 
     echo($var[0]['lat']); <<<<==== Here is the problem. 

    }; 
    ?> 

這是問題所在。第一次通過,輸出1和2下面提供的輸出和用於LAT被輸出的正確的值= 23.3

**Output 1:** Array ([0] => Array ([lat] => 23.3 [long] => 145)) 
    ------------------------------------------------------- 
    **Output 2:** 
    array (size=1) 
    0 => 
    array (size=2) 
     'lat' => string '23.3' (length=4) 
     'long' => string '145' (length=3) 

第二呼叫,產生一個錯誤Notice: Undefined offset: 0和隨後的調用都很好,也就是該陣列按預期建立。現在,如果我取消聲明waypoints[] = array();它在第一階段炸彈,但隨後所有的運行都沒問題。

有人能解釋這種行爲嗎?

謝謝

+0

我們可以假設你確實在這段代碼中有一個'session_start()',你沒有向我們展示過嗎? – RiggsFolly

+0

是的,對不起,我把它遺漏了。我現在將添加 – TrueBlue

回答

1

的問題

您的問題是在的else條件讀取$_SESSION ['waypoints'] = $waypoint['waypoint'];第一線。

的修復

如果將其更改爲$_SESSION ['waypoints'][] = $waypoint['waypoint'];,那麼你應該可以解決你所得到的錯誤。

闡釋

什麼你正在尋找echo($var[0]['lat']);是每個航點的包含數組,當他們在你的if$waypoints [] = $waypoint['waypoint'];)條件設定他們做,但不是在你的else條件($_SESSION ['waypoints'] = $waypoint['waypoint'];

+0

謝謝。是的,修復它。我同意RiggsFolly的觀點,認爲它比需要的更復雜,但是隨着我試圖解決問題的底部而增長。 – TrueBlue

4

你似乎讓生活變得比它需要的複雜一點。

session_start(); 

if (isset ($_GET ['waypoint'])) { 

    $waypoint = json_decode($_GET ['waypoint'],true); 
    print_r($waypoint['waypoint']); <<<========output 1 

    // as whatever happens we are just adding a waypoint 
    // to an array of waypoints in the SESSION variable 
    // this one line will create the $_SESSION['waypoints'][] 
    // or add a new [] to it. 

    $_SESSION['waypoints'][] = $waypoint; 
} 

// Just as belt and brace check, 
// assuming someone could try to pass this code some rubbish 
// like xxx.php&hacker=yes we ought to check before attempting to 
// use the $_SESSION['waypoints'] that one exists 

if (isset($_SESSION['waypoints']) { 
    foreach($_SESSION['waypoints'] as $var) { 
     // $var is now a waypoint array so it is not $var[0]['lat'] 
     echo sprintf('Latitude = %s - Longitude = %s', 
          $var['lat'], 
          $var['long'] 
        ); 
    } 
} 
?> 
+0

感謝您 - 特別是有關「皮帶和支架檢查」的評論,我會重寫我的代碼。 – TrueBlue

1
else { 
    $_SESSION ['waypoints'][] = $waypoint['waypoint']; 
}