In PHP, an indexed array stores key-value pairs of data using non-zero integers as keys. The first array element has a key of zero and subsequent elements are consecutively numbered. An array that uses strings as keys is called an associative array. When you attempt to access an array element with an integer key that is outside the bounds of the valid keys in an array, you receive an "undefined offset" error. The equivalent error for an associative array is an "undefined index" error.
Initializing with Array Function
One way to create an array in PHP is to use the "array" function. Pass an ordered list of elements and PHP will create an indexed array of those elements starting with zero. For example:
$dayofweek = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
echo $dayofweek; // prints "Tue"
echo $dayofweek // prints "Fri"
An "undefined offset" error in PHP occurs when you attempt to reference an array element using an array index value that doesn't exist. If you're new to programming, you may forget that indexed array element keys begin with zero, not one, and end with the number of elements in the array minus one. For example, the following code will produce an "undefined offset" error in PHP because the "dayofweek" array has seven elements at indexes zero through six, not one through seven:
Initializing Through a Function
You can also initialize an array by setting it equal to the return value of a function that returns an array, or by passing an array by reference to a function that will modify the array. For example:
$array = initialize_array(); // a function that returns an array
If you're unsure what the last element is in the array returned by the function and you attempt to access an array element after the last defined integer key, you will receive an "undefined offset" error.
Avoiding With Count
One way to avoid an "undefined offset" error is to control the integer keys you use to use to reference the array values with a loop, such as a "for" loop. Use the "count" function to identify the number of elements in an array and create a "for" loop from element zero up to the last element. For example, the following code calculates the number of elements in an array to create a loop that will not exceed the array boundary:
$numdays = count($dayofweek); // returns 7
for ($i=0; $i<$numdays; $i++) echo "<li>" . $dayofweek[$i] . "</li>"; // elements numbered zero through six
Avoiding With the "isset" Function
Another way to avoid an "undefined offset" error is to use the "isset" function. This function will return "true" or "false" depending on whether you have an array value with the specified key. It will not trigger an "undefined offset" error if the result is false. Before you attempt to reference an array element, call the "isset" function to see if it exists. For example, the following code increments a counter until it reaches an array key outside the array bounds:
$day = -1;
while (isset($dayofweek[++$day])) echo "<li>" . $dayofweek[$day] . "</li>";