1

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

1
  • Try var_dump($cats, $categories_array); and check what the data actually is you deal with. Commented Dec 20, 2012 at 22:38

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

AHA! Okay, so the problem was, indeed with the data, but I wasn't looking at it the right way. I was getting thrown off by the presence of the "Array." I was testing for the value was an array (using is_array()), but the string was literally "Array" because some of the entries in the database are actually being stored as "Array," due to a bug somewhere else in the program. So simple!
Okay, then it's the same cause but at a different location. You "stored" the bug into the database quasi. Those things are complicated to track, indeed.
BTW, to fix the problem with the bad data, I used: if (!empty($cats[$category_id])){ $file_cats_string.= $cats[$category_id].', ';}. It's like I said. Forehead-slappingly simple.
0

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

3 Comments

The foreach loop is set up correctly. This is not the problem.
I didn't say the problem was the foreach, its just about understanding the syntax of it, and using key/value pairs accordingly.
See the accepted answer and comments. Your solution is what I initially thought, too, but the problem was that the word key "Array" was literally in the data-- it was a problem with bad data, not with a bad foreach loop (you can see in the comments that the foreach loop was set up correctly, and verified).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.