【发布时间】: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