Page 1 of 1

Problem with array_key_exists

Posted: Sun Mar 05, 2006 4:30 am
by Benjamin
I have been passing values to a function which checks whether or not a key exists in an array. Everything worked fine until I starting using keys with letters. Here is an example of the array:

Code: Select all

Array
(
    [0] =>  
    [a] => blah
    [b] => blah
    [c] => blah
)
The code that checks is:

Code: Select all

function check_key($Key, $Array) {
    if (array_key_exists($Key, $Array)) {
      return true;
    } else {
      return false;
    }
}

Posted: Sun Mar 05, 2006 4:56 am
by duk
try to:

Code: Select all

function check_key($Key, $Array) {
    if (array_key_exists("$Key", $Array)) {
      return true;
    } else {
      return false;
    }
}
when you work with letters should be strings and not integers as when you work with just numbers...

Posted: Sun Mar 05, 2006 4:56 am
by Ree
You cannot use

Code: Select all

$arr[key] = 'string'
unless you have key defined as a constant. Use

Code: Select all

$arr['key'] = 'string'
instead.

Also, what exactly is the point of having a wrapper function for the native PHP one? It doesn't seem to do anything in itself right now.

Posted: Sun Mar 05, 2006 5:00 am
by Benjamin
I can't show what it does as it's proprietary but there is a lot more to it then that.

The problem was this line above it:

Code: Select all

if ($Key === 0) { unset($Key); }
It was evaluating as true until I added 3 equal signs to it. I only had 2 on it before.