What's Quicker and Better to Determine If an Array Key Exists in PHP

What's quicker and better to determine if an array key exists in PHP?

isset() is faster, but it's not the same as array_key_exists().

array_key_exists() purely checks if the key exists, even if the value is NULL.

Whereas
isset() will return false if the key exist and value is NULL.

which is faster, array_key_exists or array_search?

array_key_exists is much faster. array_search must traverse the whole array, so it is O(n). array_key_exists is a hash table lookup, so it is O(1).

See http://en.wikipedia.org/wiki/Big_O_notation if you are new to this concept.

Between array_key_exists and isset, though both are very fast [O(1)], isset is significantly faster. If this check is happening many thousands of times, you'd want to use isset.

It should be noted that they are not identical, though -- when the array key exists but the value is null, isset will return false and array_key_exists will return true. If the value may be null, you need to use array_key_exists.

Isset vs array_key_exists

See: http://us3.php.net/array_key_exists

isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

Isset vs array_key_exists

See: http://us3.php.net/array_key_exists

isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

How to check if multiple array keys exists

If you only have 2 keys to check (like in the original question), it's probably easy enough to just call array_key_exists() twice to check if the keys exists.

if (array_key_exists("story", $arr) && array_key_exists("message", $arr)) {
// Both keys exist.
}

However this obviously doesn't scale up well to many keys. In that situation a custom function would help.

function array_keys_exists(array $keys, array $arr) {
return !array_diff_key(array_flip($keys), $arr);
}

PHP check if array key exist

You just need to pass the index 'user' in the multidimensional $jsonobj array

Like this

if (array_key_exists('token', $jsonobj['user'])) {
echo "Element is in the array";
}

Modified:-

$jsonobj=json_decode($jsonobj,true);
if(!empty($jsonobj['user'])&&is_array($jsonobj['user']))
{
if (array_key_exists('token', $jsonobj['user'])) {
echo "Element is in the array";
}
}

Is (isset(..) || array_key_exists(...)) a faster way of detecting null values than just array_key_exists(...))?

Which is faster depends on the array you are checking. If the array contains a value other than null, "", or 0

if (isset(..) || array_key_exists(...)){    
}

the above code will be faster, because isset will be checked then the code executed. Array_key_exists will not be run.

If the array contains a value of null, "", or 0 then isset will be tested and then array_key_exists.
This will take longer than simply testing for array_key_exists by itself.

So the question which is faster depends a lot on the array you are checking.

A lot of people have said that it doesn't really matter. They didn't explain why it doesn't matter though. I guess they mean that the speed improvements are so minimal that it isn't worth bothering with. They may also mean that which is faster is dependent on the values assigned in your array (and thus different each time.)

Ultimately though, if you know that most of the keys will be assigned values other than null, "" or 0 and you really need to determine when null values are assigned, then use

if (isset(..) || array_key_exists(...)){    
}

Foreach - determine if key exists in each iteration

Just expose the key in the foreach and use that in your result array $test:

foreach($queryResult->records as $key => $record){
if (isset($record->Customer_Facing_Comments__r)) {
$test[$key] = 'true';
} else {
$test[$key] = 'false';
}
}


Related Topics



Leave a reply



Submit