【问题标题】:Undefined Index (Laravel)未定义索引(Laravel)
【发布时间】:2012-08-24 06:24:36
【问题描述】:

我正用头撞我的桌子,试图弄清楚为什么这个 PHP 代码会导致这个错误:Undefined index: arr。我正在使用 Laravel,这段代码在它之外就像黄金一样工作,但在 Laravel 内部,它返回未定义的索引错误。

代码如下:

function set_pilots_array($line_array)
{
    $airports = $this->airports;
    $pilots = $this->pilots;
    foreach($airports as $airport)
    {
        if($airport == $line_array[11] || $airport == $line_array[13])
        {
            if($airport == $line_array[11])
            {
                $deparr = "dep";
            }
            if($airport == $line_array[13])
            {
                $deparr = "arr";
            }
            $this->pilots[$deparr][] = array($line_array[0], $line_array[11], $line_array[13], $line_array[7], $line_array[5], $line_array[6], $line_array[8]);
        }
    }
}

function get_pilots_count()
{
    $count = count($this->pilots['dep']) + count($this->pilots['arr']);
    return $count;
}

这与我的另一个问题有关:Grab and Explode Data 它正在使用此代码从数据文件中提取数据:

elseif($data_record[3] == "PILOT")
{
    $code_obj->set_pilots_array($data_record);
}

稍后会这样做:

$code_count = $code_obj->get_pilots_count();

【问题讨论】:

  • 未定义索引!==未定义常量。您的标题非常具有误导性。
  • 你确定$this->pilots的内容吗?它是否包含键“arr”?
  • @rdlowrey,对不起,我最初是昨晚写的,我猜它保存了标题......
  • @rdlowrey 这就是 laravel 显示这个特定错误的方式:有时它确实指的是一个未定义的常量。

标签: php function foreach laravel


【解决方案1】:

好吧,代码太少,无法真正弄清楚发生了什么,但根据我所看到的:

if($airport == $line_array[13])

这个条件永远不会被满足,所以$deparr = "arr";永远不会发生,因为这个

count($this->pilots['arr']);

给出未定义的索引错误

您可以通过以下方式轻松抑制这种情况:

$count = count(@$this->pilots['dep']) + count(@$this->pilots['arr']);

【讨论】:

  • 你怎么知道这个条件永远不会满足?我们不知道$airports$pilots 的内容
  • 如果满足该条件,那么$this->pilots[$deparr] 将解析为$this->pilots['arr'],如果满足,那么您将不会收到undefined index: arr 错误...
  • @Justin,你能打印出你在var_dump 中得到的东西吗?顺便说一句,您可以通过以下方式轻松取消通知:$count = count(@$this->pilots['dep']) + count(@$this->pilots['arr']); `
  • @Chris:我知道这种普遍看法,Chris - 即使它不像每个人都说的那样那么那么糟糕。你有没有读过 PHP 内部邮件列表,了解为什么 PHP 语言中仍然存在这样的符号?
  • 而普通开发人员不得不花费数小时为它们编写检查条件,实际上是在他需要的实际代码中添加层,只是因为语言没有为他处理。许多博主/专家已经继续并美化在你的代码中添加所有这些检查条件,因此流行的看法。但是该语言保留了 @ 符号,以便他可以为不想继续编写所有这些额外检查的普通程序员提供一些影响力 - 这并不像每个人所说的那么糟糕。
【解决方案2】:

您没有设置$this->pilots['arr']。换句话说,如果您查看var_dump($this->pilots); 的输出,您将看到没有arr 键值对。我建议你解决这个问题:

$count = count((isset($this->pilots['dep']) ? $this->pilots['dep'] : array())) + count((isset($this->pilots['arr']) ? $this->pilots['arr'] : array()));

实际上,这不是一个修复 - 这更像是一个 hack。为了使您的代码正确,我建议您为 $pilots['arr']$pilots['dep'] 值设置默认值:

function set_pilots_array($line_array)
{
    $airports = $this->airports;
    $pilots = $this->pilots;

    foreach (array('dep', 'arr') as $key) 
    {
        if (!is_array($pilots[$key]) || empty($pilots[$key])) 
        {
            $pilots[$key] = array();
        }
    }

    // ...
}

【讨论】:

    【解决方案3】:

    您的问题是您直接访问所有索引而没有先检查它们是否存在。

    假设在 laravel 中某些东西导致数组没有被填充。

    为了解决这个问题,您应该使用foreach 遍历数组,或者在访问之前执行if(!empty($line_array[13])) {}

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 2015-03-23
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2021-10-06
      • 2021-01-13
      • 1970-01-01
      相关资源
      最近更新 更多