Undefined offset 1

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.

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().

The reason being is when you use list() you know the number of variables you want from the array.

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.


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.

list($var,$value) = array_pad(explode('=', $line),2,null);

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


Your are getting PHP notice because you are trying to access an array index which is not set.

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 =.