Undefined offset in 5. Iterating Associative Arrays


#1


5. Iterating of Associative Arrays


Undefined offset: 0 (line 44)


$thingsILike = array(1. => 'NYR,',
                        2. => 'kittens,',
                        3. => 'trucks,',
                        4. => 'EMS,',
                        5. => 'NYJ,',
                        6. => 'NYY,',
                        7. => 'and coding');
                        
        $length = count($thingsILike);
    
        for ($k = 0; $k < $length; $k++) {
        echo $thingsILike[$k] . '<br/>';
    }
    
        Echo '<br />Things that I like include:<br />';

Now is it because the values are different in an associtaive array?
If so can someone please explain why.


#2

I'm guessing this is line 44. Associative arrays use literal indices, not numeric as an array would.

for ($k as $v in $thingsILike) {}

Unquoted numbers might fly, but a dot is a string character if not followed by another number, as in a float. Otherwise, the way it is written above those keys will need to be in quotes.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.