What is the difference in checking an array with isset and array_key_exists?

Asked

Viewed 1,419 times

5

In PHP we often have more than one way to do the same operation. A basic example is in verifying the existence of a given index in a array: we can use array_keys_exists.

Example:

$arr = ['a' => 1, 'b' => 2];

var_dump(isset($arr['a'])); // bool(true)

var_dump(array_key_exists($arr, 'a')); //bool(true)

Considering that the results are the same, I would like to know:

  • Is there any difference between isset and array_keys_ exists when verifying the existence of the index?

  • There is a difference in performance?

  • When you should wear one or the other?

  • This helps with something http://answall.com/a/63550/3635 ?

  • 3

    In the manual says: isset() does not return TRUE for key values that match a NULL value, while array_key_exists() does this.

3 answers

7

As our friend Papa Charlie said, the return varies according to the values of the array. This code will show you a different return of the two methods cited.

<?php

$arr1 = ['a' => 1, 'b' => 2];
$arr2 = ['a' => null, 'b'=>2];

var_dump(isset($arr1['a'])); // true
var_dump(array_key_exists('a', $arr1)); // true

var_dump(isset($arr2['a'])); // false
var_dump(array_key_exists('a', $arr2)); // true

?>

isset helps you verify the existence of a key with value null.

Answering your questions...

  1. Is there any difference between isset and array_keys_existswhen checking the existence of the index?
    R: For this check, no.If the array has key and value filled in, there is no difference. If the value is null, there is difference yes.

  2. There is a difference in performance?
    A: Yes. isset() is faster, although it is not exactly the same as the other function.

  3. When you should wear one or the other?
    A: array_key_exists() checks whether the key (in this case, "a") exists. But it does not check whether the value is null or not. isset() does this check and returns false (as in the above code example).

  • 1

    The answer to the first question is wrong: If there is a difference between the two regarding the verification of existence. Because in the case of ['a' => null, 'b' => null] the indexes exist, but are null.

  • Wallace Maxters, thank you! I didn’t notice p/this. corrected answer

  • Thank you for the maturity of receiving criticism/correction. Not everyone accepts ;)

6


I gave a brief explanation in this reply /a/63550/3635 I will try to better define here the difference according to the questions asked.

Response to:

  1. Is there any difference between isset and array_keys_ exists when verifying the existence of the index?

and

  1. When you should wear one or the other?
  • array_key_exists

    The array_key_exists just check if the key exists in an array by returning a value bool, should be used only when you need to know if a specific key exists in an array, even if it has the value null she will still return true if the key exists, example:

    $foo = array( 'baz' => 0 );
    
    var_dump(array_key_exists('baz', $foo));
    
  • isset

    The isset is not a function, but the important thing is to understand how it works, the first difference is that isset supports multiple checks like this:

    isset($foo['baz'], $foo['bar'], $_POST['foo']);
    

    And of course the result varies as you add elements.

    Another difference is that he checks not only arrays, but of normal variables, for example if you do this in an undefined variable it does not send Warning:

    <?php isset($foo); ?>
    

    But this sends the message Undefined variable:

    <?php echo $foo; ?>
    

    This is because when trying to use $variável even if it does not exist it becomes "technically existing" and the value is NULL pro PHP.

    Another feature is that pro isset returns false if the variable or item of an array is null, even if there is:

    $foo = NULL;
    
    var_dump($foo);
    
    $test = array( 'foo' => NULL );
    
    var_dump($test['foo']);
    

There is a difference in performance?

This varies a lot, some say why the isset is not a function but rather a "constructor" it is likely to be faster. In most cases this may be right, even if you consider older versions of PHP, but depending on the type of check it is likely that the difference is so insignificant that it is not worth comparing (it is only a "micro optimization" at most)but what I say is that for most cases of checking, isset will be better, for example as I mentioned the possibility of checking multiple items or variables.

Note: to remove an item from an array use unset, it is possible to remove several items or variables unset($foo1, $foo2, $foo3);

Using Empty

Another function (or constructor) that might be interesting to use is the empty, it checks the value type, it will return false for when the value of the item or variable is:

  • "" (an empty string)
  • 0 (when it is an integer equal to zero)
  • "0" (zero as string)
  • NULL
  • FALSE
  • array() (an empty array)
  • public $var; (When a variable is declared in a class but has no value as it is NULL)

5

array_key_exists() will return if the key exists or not in the array, regardless of the value it has. isset() will return you bool(true) only if the key exists in the array And its value is different from null.

Another difference is that if you pass an undefined variable to the isset() he just returns bool(false), already the array_key_exists() printa one PHP WARNING on screen and returns NULL:

var_dump(isset($array['a'])); // bool(false)
var_dump(array_key_exists('a', $array)); // lança um "PHP Notice:  Undefined variable" e imprime "NULL"

$array = ['a' => null, 'b' => 2];

var_dump(isset($array['a'])); // bool(false)
var_dump(array_key_exists('a', $array)); // bool(true)

var_dump(isset($array['b'])); // bool(true)
var_dump(array_key_exists('b', $array)); // bool(true)

Therefore, use array_key_exists() when you want to know if the key exists or not in the array (regardless of the value) and you is sure that the array at least exists, and isset() when you want to make sure the key exists And that she is different from null.

See the code working on Ideone.

  • 1

    "use array_key_exists() when you want to know if the key exists or not in the array", but you know that at least the array exists.

  • @guilhermelautert corrected, Valew.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.