Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array check undefined offset php

I'll try to explain it.

i have a array:

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

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

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

Yes it works but 50 to 50 array looks like this:

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

but on the line where is the if it throws an error:

Notice: 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 :)

like image 911
Dolis Avatar asked Mar 27 '17 00:03

Dolis


People also ask

How do I fix Undefined offset in PHP?

The error can be avoided by using the isset() method. This method will check whether the declared array key has null value or not. If it does it returns false else it returns true for all cases. This type of error occurs with arrays when we use the key of an array, which is not set.

How define offset in PHP?

Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException' in Java. example: 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.

How do you check if a key exists in an array PHP?

The array_key_exists() is an inbuilt function of PHP that is used to check whether a specific key or index is present inside an array or not. The function returns true if the specified key is found in the array otherwise returns false.

What is the meaning of undefined offset?

The Offset that does not exist in an array then it is called as an undefined offset. Undefined offset error is similar to ArrayOutOfBoundException in Java. If we access an index that does not exist or an empty offset, it will lead to an undefined offset error.


2 Answers

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.

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

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

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

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

It means you are expecting a key not to exist and at the same time you are checking the value for emptyness. See the type comparison table to 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 the function that best serves your needs.

Keep in mind that by using any of these functions you are checking the value and not if the key exists in the array. You can use array_key_exists() for that.

if(array_key_exists($i, $arrayTime)){}
like image 119
Xorifelse Avatar answered Oct 06 '22 00:10

Xorifelse


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 isset method, checking if the variable is defined and isn't 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.

like image 23
Pabhoz Avatar answered Oct 06 '22 00:10

Pabhoz