PHP Notice: Undefined index when looping array...

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2009-03-14T11:24:11Z Indexed on 2010/06/17 15:13 UTC
Read the original article Hit count: 223

Filed under:
|
|

Hi, I'm looping a two-dimensional array like this:

if (!empty($aka)) {
	foreach ($aka as $ak) {
		if($ak["lang"]=="es") {
			$sptitle=$ak["title"];
		}
	}
}

Pretty simple. If the array ($aka) is not empty I loop trough it and when it finds that the "lang" index is equal to "es" I just save the "title" value for that index in $sptitle.

The problem is that the array ($aka) contains a lot of information and sometimes there is no "lang" index... and I get this error: Notice: Undefined index: lang.

How can I fix this???

This is a extract of the array to help you understand. Notice that [1] doesn't have a [lang] index but [2] does have:

[1] => Array
    (
        [title] => The Lord of the Rings: The Motion Picture
        [year] => 
        [country] => USA
        [comment] => promotional title
    )

[2] => Array
    (
        [title] => Señor de los anillos: La comunidad del anillo, El
        [year] => 
        [country] => Argentina
        [comment] => Chile, Mexico, Peru, Spain
        [lang] => es
    )

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays