未定义的偏移 PHP 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2508215/
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 PHP error
提问by user272899
I am receiving the following error in PHP
我在 PHP 中收到以下错误
Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36
注意未定义的偏移量 1:在 C:\wamp\www\includes\imdbgrabber.php 第 36 行
Here is the PHP code that causes it:
这是导致它的PHP代码:
<?php
# ...
function get_match($regex, $content)
{
preg_match($regex,$content,$matches);
return $matches[1]; // ERROR HAPPENS HERE
}
What does the error mean?
错误是什么意思?
采纳答案by Gumbo
If preg_matchdid not find a match, $matchesis an empty array. So you should check if preg_matchfound an match before accessing $matches[0], for example:
如果preg_match没有找到匹配项,$matches则是一个空数组。因此,您应该preg_match在访问之前检查是否找到匹配项$matches[0],例如:
function get_match($regex,$content)
{
if (preg_match($regex,$content,$matches)) {
return $matches[0];
} else {
return null;
}
}
回答by Eric Leschinski
How to reproduce this error in PHP:
如何在 PHP 中重现此错误:
Create an empty array and ask for the value given a key like this:
创建一个空数组并询问给定键的值,如下所示:
php> $foobar = array();
php> echo gettype($foobar);
array
php> echo $foobar[0];
PHP Notice: Undefined offset: 0 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What happened?
发生了什么?
You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.
你要求一个数组给你一个给定它不包含的键的值。它将为您提供值 NULL,然后将上述错误放入错误日志中。
It looked for your key in the array, and found undefined.
它在数组中查找您的密钥,并找到了undefined.
How to make the error not happen?
如何使错误不发生?
Ask if the key exists first before you go asking for its value.
在询问其值之前先询问密钥是否存在。
php> echo array_key_exists(0, $foobar) == false;
1
If the key exists, then get the value, if it doesn't exist, no need to query for its value.
如果key存在,则获取其值,如果不存在,则无需查询其值。
回答by pathe.kiran
Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException'in Java.
PHP 中的未定义偏移错误就像Java 中的'ArrayIndexOutOfBoundException'。
example:
例子:
<?php
$arr=array('Hello','world');//(0=>Hello,1=>world)
echo $arr[2];
?>
error: Undefined offset 2
错误:未定义的偏移量 2
It means you're referring to an array key that doesn't exist. "Offset" refers to the integer key of a numeric array, and "index" refers to the string key of an associative array.
这意味着您指的是一个不存在的数组键。“偏移”指的是数值数组的整数键,“索引”指的是关联数组的字符串键。
回答by Felix Siaw-Yeboah
Undefined offset means there's an empty array key for example:
未定义的偏移量意味着有一个空数组键,例如:
$a = array('Felix','Jon','Java');
// This will result in an "Undefined offset" because the size of the array
// is three (3), thus, 0,1,2 without 3
echo $a[3];
You can solve the problem using a loop (while):
您可以使用循环(while)解决问题:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
// Increase count by 1, thus, $i=1
$i++;
$groupname[$i] = base64_decode(base64_decode($row['groupname']));
// Set the first position of the array to null or empty
$groupname[0] = "";
}

