php 数组检查未定义的偏移量php

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

Array check undefined offset php

phparraysif-statementfor-loop

提问by Dolis

I'll try to explain it.

我会试着解释一下。

i have a array:

我有一个array

 $arrayTime = array(0=>"07",1=>"09", 3=>"13", 4=>"15", 5=>"17", 6=>"19");

Here you can see that is not defined offset 2and now i need formy arrayand on offset 2push number 0(for example) I tried use this:

在这里你可以看到它没有定义offset 2,现在我需要for我的arrayoffset 2推送数字 0(例如)我尝试使用这个:

if($arrayTime[$i]==""){
   $arrayTime[$i]=0;
}

Yes it works but 50 to 50 arraylooks like this:

是的,它有效,但 50 到 50array看起来像这样:

$arrayTime = array(0=>"07",1=>"09", 3=>"13", 4=>"15", 5=>"17", 6=>"19",2=>"0");

but on the line where is the ifit throws an error:

但在它在哪里if引发错误的那一行:

Notice: Undefined offset: 2 in C:\wamp\www\xxx.php on line 10

注意:Undefined offset: 2 in C:\wamp\www\xxx.php on line 10

So i need same result, but without error. Thanks for your help all :)

所以我需要相同的结果,但没有错误。感谢大家的帮助:)

回答by Xorifelse

First of all, it doesn't throw an error. It gives you a warning about a possible bug in your code.

首先,它不会抛出错误。它会警告您代码中可能存在的错误。

if($arrayTime[$i]==""){}

This attempts to access$arrayTime[$i]to retrieve a value to compare against your empty string.

这会尝试访问$arrayTime[$i]以检索一个值以与您的空字符串进行比较。

The attempt to readandusea non-existing array indexto get a value for comparison is the reason why it throws the warning as this is usually unexpected. When the key does not exist nullis used instead and the code continues executing.

要尝试阅读使用一个不存在的数组索引,以获取比较的价值就是它抛出的警告,因为这通常是意外的原因。当密钥不存在时null,将改为使用并且代码继续执行。

if(null == ""){} // this evaluates to true.

Because you are comparing against an empty string "", your answer would be empty():

因为您正在与空字符串进行比较"",所以您的答案是empty()

if(empty($arrayTime[$i])){}

It means you are expecting a key not to exist and at the same time you are checking the valuefor emptyness. See the type comparison tableto see what is and what is not considered 'empty'.

这意味着您期望键不存在,同时您正在检查是否为空。请参阅类型比较表以了解什么是“空”,什么不是“空”。

The same rules apply to isset()and is_null(), it wont throw the notice if the key does not exist. So choose function that best serves your needs.

同样的规则适用于isset()and is_null(),如果键不存在,它不会抛出通知。因此,请选择最能满足您需求的功能。

Keep in mind that by using any of these functions you are checking the valueand notif the keyexists in the array. You can use array_key_exists()for that.

请记住,使用任何这些功能正在检查的价值没有如果数组中存在。你可以用array_key_exists()它。

if(array_key_exists($i, $arrayTime)){}

回答by Pabhoz

to add zeroes to your non-defined indexes without getting a Notice you should evaluate if the desired index to compare exists, so instead of comparing directly try checking the existence of the index first by using issetmethod, checking if the variable is defined and isn't NULL.

在没有得到通知的情况下向未定义的索引添加零,您应该评估要比较的所需索引是否存在,因此不要直接比较尝试首先使用isset方法检查索引的存在,检查变量是否已定义并且是不是 NULL。

So your code to validate should look like this:

因此,您要验证的代码应如下所示:

    //check for the index before tryin' to acces it
    if( !isset($arrayTime[$i]) ){
       $arrayTime[$i]=0;
    }

Hope it works for you.

希望对你有效。