php 未定义索引 (Laravel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12181386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined Index (Laravel)
提问by Dutchcoffee
I'm bashing my head against my desk trying to figure out why this PHP code is causing this error: Undefined index: arr. I'm using Laravel, and this code works like gold outside of it, but inside Laravel, it's returning the undefined index error.
我正用头撞在我的桌子上,试图弄清楚为什么这个 PHP 代码会导致这个错误:Undefined index: arr. 我正在使用 Laravel,这段代码在外面就像黄金一样工作,但在 Laravel 内部,它返回了未定义的索引错误。
Here's the code:
这是代码:
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;
}
This sort of goes with my other question: Grab and Explode DataIt's pulling the data from the data file using this code:
这种类型与我的另一个问题相吻合:Grab and Explode Data它使用以下代码从数据文件中提取数据:
elseif($data_record[3] == "PILOT")
{
$code_obj->set_pilots_array($data_record);
}
Which later does this:
稍后这样做:
$code_count = $code_obj->get_pilots_count();
回答by shybovycha
You do not have $this->pilots['arr']set. In other words, if you look at the output of var_dump($this->pilots);, you shall see there is no arrkey-value pair. I suggest you this fix:
你没有$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()));
Actually, this is not a fix - this is more like a hack. To make your code correcti suggest you to set the default values for those $pilots['arr']and $pilots['dep']values:
实际上,这不是修复 - 这更像是一种黑客攻击。为了使您的代码正确,我建议您为这些$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();
}
}
// ...
}
回答by Kristian
your problem is that you are accessing all of your indexes directly without checking if they exist first.
您的问题是您直接访问所有索引,而没有先检查它们是否存在。
assume that in laravel something is causing the array to not be populated.
假设在 laravel 中某些东西导致数组没有被填充。
in order to fix this, you should either iterate through the array with a foreach, or do a if(!empty($line_array[13])) {}before accessing it.
为了解决这个问题,您应该使用 a 遍历数组foreach,或者if(!empty($line_array[13])) {}在访问它之前执行 a 。
回答by raidenace
Well there is too little code to really figure out what is going on, but based on what I see:
好吧,代码太少,无法真正弄清楚发生了什么,但基于我所看到的:
if($airport == $line_array[13])
this condition is never being met and so $deparr = "arr";never happens and because of this
这个条件永远不会被满足,所以$deparr = "arr";永远不会发生,因此
count($this->pilots['arr']);
is giving an undefined index error
给出未定义的索引错误
You can easily suppress this by:
您可以通过以下方式轻松抑制这种情况:
$count = count(@$this->pilots['dep']) + count(@$this->pilots['arr']);

