Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of array_intersect() in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 127
    Comment on it

    The array_intersect() is a built-in function in PHP used for computing the intersection of two or more arrays. The function takes two or more arrays as arguments, after comparing values of arrays the function returns an array containing all values from first array which are also present in other arrays. So the resultant array contains the values which are common in all the arrays passed as arguments.


    Note : array_intersect() preserved the keys.


    Syntax : array_intersect(array1, array2, array3,.....)

    array1 : An array which compared with other arrays.(Required)
    array2 : An array which compared with array1.(Required)
    array3 : Another array which compared with array1.(Optional)


    Example :

    <?php
    $array1 = array("a" => "orange", "apple", "mango", "pear");
    $array2 = array("b" => "peach", "orange", "mango", "banana");
    $result = array_intersect($array1, $array2);
    print_r($result);
    ?>
    

    Output : Array ( [a] => orange [1] => mango )


 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: