Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between array_key_exists() and in_array()

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 995
    Comment on it

    array_key_exists() and in_array() are two most useful functions in PHP. This article demonstrate the details and differences of these function.

     

    array_key_exists() : This function checks if the given key or index exists in array or not. If key found in array it returns TRUE and return FALSE if key does not exist in array.

     

    Syntax : bool array_key_exists ($key , $array)

    key : Specifies the key/index to be search. Required
    array : Specifies an array. Required

     

    Example :

    <?php
    $array = array(1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four');
    if (array_key_exists(2, $array)) {
        echo "Key exist in the array";
    }
    ?>

    Output :

    Key exist in the array

     

    Difference between isset() and array_key_exist()

    The best way to check an index exist in array or not is using array_key_exists(). isset() is another most frequently used function to check  a variable is set or not, isset() checks whether a variable is set and is not null. So the keys which corresponds to null value in array isset() return false.

     

    Example :

    <?php
    $array = array(1 => 'One', 2 => null, 3 => 'Three', 4 => 'Four');
    
    if(isset($array[2]))
    {
      echo "True";
    }else{
      echo "False";
    }
    echo "<br>";
    echo array_key_exists(2, $array);
    
    ?>

    Output :

    False
    1

     

    in_array() : in_array() function checks a value exists in array or not. If value found in array the function return true and false on failure.

     

    Syntax : in_array ($value, $array, $strict_mode)

    value : Specifies the value to be search in array. Required
    array : Specifies an array to search. Required
    strict_mode : If this parameter is set to TRUE, the in_array() function also check the types of $value in $array. Optional

     

    Example :

    <?php
    $countries = array("India", "USA", "France", "Germany");
    if (in_array("India", $countries)) {
        echo "India exists in array";
    }
    ?>

    Output :

    India exists in array

     

    Example : in_array() with strict

    <?php
    $countries = array("India", 12, "France", 10);
    if (in_array('10', $countries, True)) {
        echo "Value exists in array";
    }else{
       echo "Type not match with records in array";
    }
    ?>

    Output :

    Type not match with records in array

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: