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
.
Whereasisset()
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 returnTRUE
for array keys that correspond to aNULL
value, whilearray_key_exists()
does.
Isset vs array_key_exists
See: http://us3.php.net/array_key_exists
isset()
does not returnTRUE
for array keys that correspond to aNULL
value, whilearray_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
With "Magic Quotes" Disabled, Why Does PHP/Wordpress Continue to Auto-Escape My Post Data
Decode Gzipped Web Page Retrieved Via Curl in PHP
Session Data Lost in Chrome Only
Why Doesn't MySQL Support Millisecond/Microsecond Precision
Why Is This PHP Call to Json_Encode Silently Failing - Inability to Handle Single Quotes
Prevent Sent Emails Treated as Junk Mails Using PHP Mail Function
Adding Months to a Date in JavaScript
How to Strip a Tag and All of Its Inner HTML Using the Tag'S Id
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
How to Use a String as an Array Index Path to Retrieve a Value
How to Split a String by Multiple Delimiters in PHP
Verify Receipt For in App Purchase
How to Use PHPexcel to Read Data and Insert into Database
How to Set the Default Value of a Timestamp Column to the Current Timestamp With Laravel Migrations
How to Install Composer PHP Packages Without Composer