mamdouh asked this 7 years ago

PHP How to check if a key exists in an associative array?

Should i use isset() or array_key_exists()?


Best Answer by pseudoFish 7 years ago

$arr["foo"] = bar;

You can use both

if(isset($arr["foo"]))
{//do something}

or

if(array_key_exists("foo",$arr))
{// do something}

But if 

$arr["foo"] = null;

isset($arr["foo"])
returns FALSE

array_key_exits("foo",$arr)
returns TRUE

So it depends on what action you want to take if value is NULL at that key.