This Season
 

Unidentified Index Errors in PHP

In PHP, an array is a data structure composed of key-value pairs. The key, also called an index, identifies where in the array the element resides. The value is the value of the element. For example, consider the statement

$x[12] = 100;

"12" is the array index and "100" is the value of the element at index "12." In the statement:

$x['name'] = "Mark";

"name" is the array index and "Mark" is the value of the element at that index. An "unidentified index" error occurs in PHP when you attempt to reference the value of an array element using an index that does not exist in the array.

Related Searches:
    1. Typographical Errors

      • Sometimes typographical errors cause array index problems.You may create a loop to perform an operation on each element in an array and mistype the variable you are using to specify the index. For example, consider the following code snippet that causes an "unidentified index" error because the variable was mistyped in the body of the loop.

        for ($index=0; $index<count($array); $index++) {
        $array[$idnex] += 55;
        }

      Associative Array Indexes

      • An associative array allows you to use alphanumeric keys for indexes. It's important to understand that these keys are case sensitive, and that the following code will produce an "unidentified index" error:

        $array['x'] = 5;
        echo $array['X'];

        You can avoid these errors by transforming the case of the index variable using the "strtolower" function, as in the following example:

        foreach ($letters as $letter) {
        echo $array[strtolower($letter)];
        }

      Bad Data

      • An "unidentified index" error can sometimes occur because of bad data. For example, if you are processing a data set in which you expect all index values to be non-negative and you encounter a negative value in the data set, it will cause an "unidentified index" error. To prevent the error from occurring, set a condition to trap illegal array indexes before they are used, as in the following example:

        while (($row = mysql_fetch_assoc($result))) {
        if ($row["array_index"] < 0) {
        echo "Bad array index value of " . $row["array_index"];
        } else {
        echo $array[$row["array_index"]];
        }

      Preventing the Error in Valid Cases

      • Sometimes you have an array of elements and you want to check if a particular item is in the array. For example, consider this array of items and the quantity of each item on hand:

        $item['candy'] = 100;
        $item['gum'] = 75;
        $item['mints'] = 50;

        If you want to check to see if you have any lozenges, the following statement will cause an "unidentified index" error because there is no "lozenges" item in the array.

        echo $item['lozenges'];

        To prevent this error, use the "isset" function to first check if the element is in the array before you check the quantity value, as in the following code:

        if (isset($item['lozenges']) echo $item['lozenges'];

    Related Searches

    References

    Read Next:

    Comments

    Follow eHow

    Related Ads