Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Compute intersection of two or more arrays

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 204
    Comment on it

    My previous article Use of array_intersect() in PHP demonstrates about array_intersect() function which is used to compute the intersection of two or more arrays. array_intersect() returns an array with values of first array which are also present in other arrays, the comparison is made on the basis of values only. This article demonstrates other functions which computes intersection of two or more arrays on basis of the keys and on the basis of both keys and values.

     

    1. array_intersect_assoc()

    Just like array_intersect(), array_intersect_assoc() function also used for computing the intersection between two or more arrays. This function compares the keys and the values of first array with other arrays and returns an array of matching elements.

     

    Result : This function returns an associative array of elements from first array which are matched with elements of other arrays. This function used keys also for comparison.

     

    Syntax : array_intersect_assoc( $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", "b" => "apple", "c" => "mango", "d" => "pear");
    $array2 = array("b" => "peach", "a" => "orange", "c" => "mango", "d" => "banana");
    $result = array_intersect_assoc($array1, $array2);
    print_r($result);
    ?>
    

    Output :

    Array ( [a] => orange [c] => mango )

     

     

    2. array_intersect_key()

    array_intersect_key() function also used for computing the intersection of two or more arrays using key comparison. This function takes two or more arrays as arguments and compare the keys of the first array with other arrays, It returns an array of matching elements of the first array.

     

    Result : This function returns an array with elements from the first array on the basis of comparison of keys between all arguments. It returns FALSE on failure.

     

    Syntax : array_intersect_key($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("orange" => "1", "apple" => "3", "mango" => "2", "pear" => "5");
    $array2 = array("orange" => "3", "mango" => "5", "peach" => "4", "apple" => "1");
    $array3 = array("orange" => "4", "mango" => "2", "peach" => "6");
    $result = array_intersect_key($array1, $array2, $array3);
    print_r($result);
    ?>

    Output :

    Array ( [orange] => 1 [mango] => 2 )

 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: