php 未定义的偏移量 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16728268/
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 offset 1
提问by tushAR
Undefined offset: 1
未定义偏移:1
Hello ... I am facing a problem that undefined offset :1 in line 3. I can't understand that what type of error it is. Can anyone tell me that why such error occurs in php
您好...我在第 3 行遇到了 undefined offset :1 的问题。我不明白这是什么类型的错误。谁能告诉我为什么在 php 中会发生这样的错误
Undefined offset in line : 3
行中未定义的偏移量:3
foreach ($lines as $line)
{
list($var,$value) = explode('=', $line); //line 3
$data[$var] = $value;
}
回答by Nauphal
Your are getting PHP notice
because you are trying to access an array index which is not set.
您正在获取 PHP,notice
因为您正在尝试访问未设置的数组索引。
list($var,$value) = explode('=', $line);
The above line explodes the string $line
with =
and assign 0th
value in $var
and 1st
value in $value
. The issue arises when $line
contains some string without =
.
上述线爆串$line
与=
和分配0th
在值$var
和1st
在值$value
。当$line
包含一些没有=
.
回答by mrjamesmyers
I know this an old question and the answer provided is sufficient.
我知道这是一个老问题,提供的答案就足够了。
Your are getting PHP notice because you are trying to access an array index which is not set.
您收到 PHP 通知是因为您试图访问未设置的数组索引。
But I believe the best way to overcome the problem with undefined indexes when there are cases where you may have an empty array using the list()
/explode()
combo is to set default values using array_pad()
.
但我相信,在使用list()
/explode()
组合可能有空数组的情况下,克服未定义索引问题的最佳方法是使用array_pad()
.
The reason being is when you use list()
you know the number of variables you want from the array.
原因是当您使用时,list()
您知道需要从数组中获得的变量数量。
For example:
例如:
$delim = '=';
$aArray = array()
$intNumberOfListItems = 2;
list($value1, $value2) = array_pad(explode($delim, $aArray, $intNumberOfListItems ), $intNumberOfListItems , null);
Essentially you pass a third parameter to explode stating how many values you need for your list()
variables (in the above example two). Then you use array_pad()
to give a default value (in the above example null) when the array does not contain a value for the list variable.
本质上,您传递第三个参数来说明您的list()
变量需要多少个值(在上面的示例中)。然后,array_pad()
当数组不包含列表变量的值时,您可以使用默认值(在上面的示例中为 null)。
回答by Stan Fad
This is caused because your $line
doesn't contain "=
" anywhere in the string so it contains only one element in array.list() is used to assign a list of variables in one operation. Your list contains 2 elements but as from data returned by implode, there is only one data. So it throws a notice.
A way to overcome that is to use array_pad()method.
这是因为您在字符串中的任何地方$line
都不包含“ =
”,因此它只包含 array.list() 中的一个元素,用于在一个操作中分配变量列表。您的列表包含 2 个元素,但从 implode 返回的数据来看,只有一个数据。所以它会抛出一个通知。克服这个问题的一种方法是使用array_pad()方法。
list($var,$value) = array_pad(explode('=', $line),2,null);
回答by Roberto Arosemena
by doing list($var, $value) php will expect an array of 2 elements, if the explode function doesn't find an equal symbol it will only return an array with 1 element causing the undefined offset error, offset 1 is the second element of an array so most likely one of your $line variables doesn't have an equal sign
通过执行 list($var, $value) php 将期望一个包含 2 个元素的数组,如果爆炸函数没有找到等号,它将只返回一个包含 1 个元素的数组,导致未定义的偏移错误,偏移 1 是第二个数组的元素,因此您的 $line 变量之一很可能没有等号
回答by Prakash
This is due to the array. The array index is not showing due to this undefine offset error will come...
这是数组的原因。由于此未定义偏移量错误将出现,数组索引未显示...
So please check the array with print_r function.
因此,请使用 print_r 函数检查数组。
回答by leftclickben
The list
language construct is used to create individual variables from an array. If your array doesn't have enough elements for the number of variables you are expecting in the list
call, you will get an error. In your case you have 2 variables so you need an array with 2 items - indexes 0 and 1.
的list
语言结构被用来从一个阵列创建单个变量。如果您的数组没有足够的元素来满足您在list
调用中期望的变量数量,您将收到错误消息。在您的情况下,您有 2 个变量,因此您需要一个包含 2 个项目的数组 - 索引 0 和 1。
回答by Mahesh
Solution:
解决方案:
$lines = array('one' => 'fruit=apple', 'two' => 'color=red', 'three' => 'language');
foreach ($lines as $line)
{
list($var,$value) = (strstr($line, '=') ? explode('=', $line) : array($line, ''));
$data[$var] = $value;
}
print_r($data);
Try this one..
试试这个..
For reference http://in1.php.net/manual/en/function.list.phphttp://in1.php.net/manual/en/function.explode.php
供参考 http://in1.php.net/manual/en/function.list.php http://in1.php.net/manual/en/function.explode.php