reubengrafy
reubengrafy

Reputation: 151

Undefined index: Array, when the index is defined

This is making me crazy, because I need E_ALL turned on for other reasons. I can't get rid of this bug.

Here is my function:

public static function getFileCatsString($categories)
{
    if (empty($categories)) {
        return '';
    }
    $cats = self::getFileCats();

    $file_cats_string = '';
    $categories_array = explode(',',$categories);
    foreach($categories_array as $k=>$category_id) {
        $file_cats_string.=$cats[$category_id].', ';
    }
    $file_cats_string = rtrim($file_cats_string, ', ');
    return($file_cats_string);
}

Categories are stored in keyed array: [id]=>[string]

$categories that gets passed is a string of category_ids (long story as to why that is)

the getFileCats() method gets a list of all available categories.

So we explode the commad list to create an array, and then we loop through that array. I simply want to create, a string of category labels when I'm given a string of category ids. PHP returns this warning:

Notice: Undefined index: Array in Documents.php on line 40

Line 40 is:

      $file_cats_string.=$cats[$category_id].', ';

So obviously the undefined index is $cats[$category_id]. But here's where it gets weird.

If I use a die() statement and echo out $cats[$category_id] I indeed get a string, not an array.

By the way, here is the output of each of the three key pieces of data with a die() statement put at the top of the foreach loop.

$categories_array:

Array ( [0] => 2 )

$cats:

Array ( [9] => Category 19 [8] => Category 8 [7] => Category 7 [6] => Another String I Changed For Privacy [5] => AED Sales [4] => Preceptor Folder [3] => Education Brochures [2] => Forms [1] => Guidelines and Policies )

$cats[$category_id]

Forms

$category_id

2

It gets weirder though. It says that the index is an array (which would indeed be a problem), so I tried putting:

        if (is_array($category_id)) {
            die(print_r($categories_array, true) . '<br />' . print_r($cats, true) . '<br />' . $cats[$category_id] . '<br />' . $category_id);
        }

to see if I could identify a piece of defending data, but it never dies. So what in the heck is going on?! (I have a sneaky suspicion that the answer is going to be forehead-slappingly simple.)

Upvotes: 1

Views: 3962

Answers (2)

hakre
hakre

Reputation: 197682

The error is easier to understand knowing the fact that converting any array in PHP to a string results in the string:

"Array"

(this is also outlined in Type Juggling in the PHP manual)

Exactly that happens in line 40:

$file_cats_string .= $cats[$category_id] . ', ';
                           ^^^^^^^^^^^^

The variable $category_id at that point is an array. Fix that. Valid array indexes/keys are integer numbers or strings but not arrays.

Upvotes: 3

Teena Thomas
Teena Thomas

Reputation: 5239

In a foreach loop, the syntax foreach($categories_array as $k=>$category_id) { means, $k is the key and $category_id is the value per index/key. Hence, the array doesn't have a value with one of its value as index, that's why the error says undefined index.

Since $category_id is NOT an array, and its got a value 2, it doesnt pass the check of if (is_array($category_id))

Upvotes: 0

Related Questions