Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • in_array and array_key_exists in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 151
    Comment on it

    Hello Readers

    In php there is a great role of php array, we will discuss about the in_array and array_key_exists in php with examples.

    in_array in php:

    in_array: in_array in php checks the array value it is exist or not to the given array.

    It support the php verison 4 or above.

    syntax of in_array in php:

    bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] );

    The above Parameters are:

    needle: The searched value.

    haystack: The array.

    strict:If the 3rd param set true then the above array function (in_array) check the types of the needle.

    So, in this if needle is found into the given array returns true, otherwise it returns false.

    For Example:

    $array = array("Web", "Technology", "Iphone", "Java");
    if (in_array("Technology", $array)) {
        echo "Record Found in array";
    }
    

    In the above example, there is a array contain the four value, and then we use the array function(in_array) to check the particular value exists in an array is found its return (return type) is true and print the message otherwise return false.
    array_key_exists in php:

    array_key_exists is the other array function in php which is used to checks is the given key or index exists in the array.

    It support php version 4 or above.

    syntax of array_key_exists in php:

    bool array_key_exists ( mixed $key , array $array );

    Parameters:

    key: Value to check.

    array: An array with keys to check.

    It also return true on success or false on failure.

    For Example:

    $searchArray = array('Web' => 3, 'Technology' => 5);
    if (array_key_exists('Web', $searchArray)) {
        echo "Record Found in key";
    }
    

    In the above code we use the array (array_key_exists) and check the key of the particular value of the given array. If it is found return true else return false

 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: