PHP:未定义的偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25194234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:43:19  来源:igfitidea点击:

PHP: Undefined offset

phpundefinedoffsetexplodenotice

提问by n1k1c4

On some pages, i receive the error:

在某些页面上,我收到错误消息:

PHP Notice: Undefined offset: 1 in /var/www/example.com/includes/head.php on line 23

PHP 注意:未定义的偏移量:1 in /var/www/example.com/includes/head.php on line 23

Here is the code:

这是代码:

if ($r)
{

    list($r1, $r2)=explode(" ", $r[0],2);
    $r1 = mb_strtolower($r1);
    $r3 = " ";
    $r2 = $r3.$r2;
    $r[0] = $r1.$r2;
    $page_title_f = $r[0]." some text";
    $page_title_s = "some text ";
    $page_title = $page_title_s.$page_title_f;

}

Line 23 with error:

有错误的第 23 行:

list($r1, $r2)=explode(" ", $r[0],2);

Could you help to understand what could be the problem?

你能帮助理解可能是什么问题吗?

Update

更新

Thanks all for the help! I partially solved the problem.

感谢大家的帮助!我部分解决了这个问题。

$ris row in the database. The script takes a string and starts to manipulate. Converts uppercase letters to lowercase. And as I understand it, the string must have a space otherwise gets out an error "Undefined offset". Because the script tries to find the first space, and then merge the word before the first space, and the space together with everything that appears after a space. (: I don't understand why he does and there is no way out of this situation if the space in the string no, he just throws an error. ): In general it is a very old and poor engine web store called Shop-Script. Post a full listing of the file, maybe it will be clearer.

$r是数据库中的行。该脚本接受一个字符串并开始操作。将大写字母转换为小写字母。据我了解,该字符串必须有一个空格,否则会出现错误“未定义的偏移量”。因为脚本试图找到第一个空格,然后将第一个空格之前的单词合并,并将空格与空格之后出现的所有内容合并。(:我不明白他为什么这样做,如果字符串中的空格为 no,则无法摆脱这种情况,他只是抛出一个错误。):总的来说,这是一个非常老旧的引擎网络商店,名为 Shop-脚本。发布文件的完整列表,也许会更清楚。

http://pastebin.com/Pz1TKpr3

http://pastebin.com/Pz1TKpr3

回答by Darren

The undefined index error you are receiving is because the offset that you're trying to explode in the variable ($r) doesn't exist.

您收到的未定义索引错误是因为您尝试在变量 ( $r) 中爆炸的偏移量不存在。

You can check what $ris by doing the following:

您可以$r通过执行以下操作来检查是什么:

print_r($r);

or

或者

var_dump($r);

You'll need to show what $rholds before debugging this issue further.

$r在进一步调试这个问题之前,您需要展示什么是成立的。

But a guess would be that your $rvariable is a string that you're trying to explode, but you're trying to access it as an array.

但是猜测您的$r变量是一个您试图分解的字符串,但您试图将其作为数组访问。

What happens if you explode it like this:

如果你像这样爆炸它会发生什么:

list($r1, $r2) = explode(' ', $r, 2);

回答by Cobra_Fast

list($r1, $r2) = explode(" ", $r[0],2);

When supplied with two variables, list()will need an array with at least 2 items.
However, as the error you mentioned indicates, your explode()call seems to return an array with only one item.

当提供两个变量时,list()将需要一个至少包含 2 个项目的数组。
但是,正如您提到的错误所示,您的explode()调用似乎返回了一个只有一项的数组。

You will have to check the contents of $r[0]to make sure it actually contains your splitting character or manually assign $r1and $r2with sanity checks.

您必须检查 的内容$r[0]以确保它确实包含您的拆分字符或手动分配$r1$r2进行完整性检查。

回答by Barmar

Try:

尝试:

$words = explode(' ', $r[0], 2);
$r1 = isset($words[0]) ? $words[0] : '';
$r2 = isset($words[1]) ? $words[1] : '';

If $r[0]doesn't contain 2 words, your code will get an error because explode()will return an array with just one element, and you can't assign that to two variables. This code tests whether the word exists before trying to assign it.

如果$r[0]不包含 2 个单词,您的代码将出错,因为explode()将返回一个只有一个元素的数组,并且您不能将其分配给两个变量。此代码在尝试分配之前测试该词是否存在。

回答by Jeffry Louis

My answer is not specific to your code. For others who get this error while trying to use specific elements of array you created, try indexing your elements while creating the array itself. This helps you to explode/print a specific element/do whatever you want with the array.

我的答案不是特定于您的代码。对于在尝试使用您创建的数组的特定元素时遇到此错误的其他人,请尝试在创建数组本身时索引您的元素。这有助于您分解/打印特定元素/对数组执行任何您想要的操作。