Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check a key or value exist in array or not

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 241
    Comment on it

    This article demonstrates about PHP functions which can be used to check whether a key or value exist in an array or not. The details of each function are given below :

    1. Checking a value in array
    1.1 in_array()

    in_array() is the most popular function to check if a value exists in an array or not. The function searches for a specified value in an array.


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

    $value : Specifies  value which has to be searched in the array.(Required)

    $array : Specifies an array.(Required)

    $strict : Defines search mode. If it is set to true the function also checks the type of $value in the array.(Optional)


    Description : The in_array() function takes three arguments and search the specified value in an array. If the third argument is set to TRUE, the function also checks the type of search value in the array.


    Result : If searching value is found in an array, in_array() function returns TRUE and on failure it returns FALSE.


    Note :

    1. From PHP version 4.2 and above search value can also be an array.

    2. If the search value is a string and strict mode is true than search is performed in case-sensitive manner.


    Example1 :

    <?php
    $arr = array("20","ram","10","deepak");
    if (in_array(10, $arr))  
    {  
      echo "10 exist in array";  
    }  
    ?>

    Output : 10 exist in array


    Example 2 :

    <?php
    $arr = array("20","ram","10","deepak");
    if (in_array(10, $arr, TRUE))  
    {  
      echo "10 exist in array";  
    }else{
     echo "10 not exist in array";  
    }  
    ?>

    Output : 10 not exist in array


    1.2 array_search()

    array_search() function also used  to check the existence of a value in the array. This function search a specified value and returns its corresponding key.


    Syntax : array_search($value, $array, $strict)

    $value : Specifies the value which has to be searched in the array.(Required)

    $array : Specifies an array.(Required)

    $strict : Defines search mode, If it is set to TRUE the function also checks the type of $value in the array. The default value is FALSE. (Optional)


    Description : The array_search() function also takes three arguments and search for the specified value in the array. If the third argument is set to TRUE, the function also checks the type of search value.


    Result : If searching value is found in an array, array_search() function returns the corresponding key and on failure it return FALSE. If specified search value found more than once in the array, then function returned the first matching key.


    Note :

    1. If the search value is of string type, the function performs searches in a case-sensitive manner.

    2. For PHP versions before 4.2.0, the function returns NULL instead of FALSE on failure.

    3. The function returns NULL with invalid parameters (This applies to all internal functions of PHP from PHP version 5.3.0).


    Example :

    <?php
    $arr = array("a" => "2", "b" => "5", "c" => "10", "d" => "20");
    $index = array_search(10, $arr);
    echo $index."<br>";
    $arr1 = array("a" => "2", "b" => "5", "c" => "2", "d" => "20");
    $index2 = array_search(2, $arr1);
    echo $index2;
    ?>

    Output :
    10 found at index : c
    2 found at index : a

     

    2. Checking a key in array
    2.1 array_key_exists()

    array_key_exists() is a predefined function in PHP which can be used to check whether an index exist in an array or not.


    Syntax : array_key_exists($key, $array)

    $key : Represent index value which has to be checked.(Required)

    $array : Defines an array.(Required)


    Result : array_key_exists() searches for a key in an array. The function returns TRUE on success and FALSE on failure.


    Example :

    <?php
    $arr = array('blue' => 2, 'green' => 5, 'red' => 1, 'black' => 10);
    if (array_key_exists('green', $arr)) {
        echo "The key 'green' exist in the array";
    }
    ?>

    Output : The key 'green' exist in the 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: