【问题标题】:Building an array gives PHP Undefined Offset: 0建立一个数组给 PHP Undefined Offset: 0
【发布时间】:2016-09-25 14:29:47
【问题描述】:

我可能可以找到解决这个问题的方法,但我真的很想了解是什么语言结构导致了它。

逻辑如下,我想在用户点击谷歌地图上的路线时建立一个会话变量,该路线是一个航点数组。 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 的正确值是 output = 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(); 语句,它会在第一次通过时爆炸,但所有后续运行都可以。

有人能够解释这种行为吗?

谢谢

【问题讨论】:

  • 我们可以假设您在此代码中确实有一个session_start(),但您没有向我们展示吗?
  • 是的,很抱歉我漏掉了。我现在添加

标签: php arrays


【解决方案1】:

你似乎让生活变得比它需要的复杂一点。

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']
                    );
    }
}
?>

【讨论】:

  • 谢谢你——尤其是关于“皮带和支撑检查”的评论,我将重新编写我的代码。
【解决方案2】:

问题

您的问题出在else 条件的第一行,即$_SESSION ['waypoints'] = $waypoint['waypoint'];

修复

如果您将其更改为$_SESSION ['waypoints'][] = $waypoint['waypoint'];,那么您应该解决您遇到的错误。

解释

您在 echo($var[0]['lat']); 中寻找的是每个航路点的 包含一个数组,当它们在您的 if ($waypoints [] = $waypoint['waypoint'];) 条件下设置时它们会执行但不是在您的else 条件下 ($_SESSION ['waypoints'] = $waypoint['waypoint'];)

【讨论】:

  • 谢谢。是的,解决了它。我同意 RiggsFolly 的观点,它比它需要的更复杂,但是随着我试图找出问题的根源,它变得越来越复杂。
【解决方案3】:
else {
    $_SESSION ['waypoints'][] = $waypoint['waypoint']; 
}

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 2018-07-23
    • 1970-01-01
    • 2014-11-09
    • 2017-01-14
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多