PHP | array_intersect_uassoc() Function

The array_intersect_uassoc() function is an inbuilt function in PHP. It is used to compare key and values of two or more arrays by using a user-defined comparison function and return the matches.

The comparison function returns an integer equal to, greater than or less than zero. If the first argument to be considered respectively less than, equal to or greater than second. If condition true then return TRUE value, else it returns FALSE value.

Syntax:

array_intersect_uassoc($array1, $array2, $array3..., uassoc_intersectFunction)

Parameters Used: This function accepts minimum three parameters and all three parameter is mandatory and the others parameters are optional. Parameters are described below:

  1. $array1 (Required ):
    The array will be compared with other arrays..
  2. $array2 (Required):

    The array Compared with the first array.

  3. $array3…(Optional):

    The array Compared with the first array.

  4. uassoc_intersectFunction (Required):

    It is Required user-defined function. A string that defines a callable comparison function. The comparison function returns an integer less than, equal to or greater than 0. If the first argument is less than, equal to or greater than the second argument.



Return Value:
Returns an array containing the entries from array1 that are present in all other arrays such as:-(arra2, arra3, arar4….more). The return value type is an array.

Note: The function uses a user-defined function to compare the keys. (user-define function's
functionality is applicable for key not for values of keys)

Let’s take an example to understand the array_intersect_uassoc() Function.
Program: 1 In this example we comare the keys of array by using case-insensitive strcasecmp function. It compares keys without case-sensitiveness.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$arr1 = array(
    "a" => "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "x" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
  
  
  
// Compare the keys and values by using a 
// user-defined key comparison function.
// Here callback function applicable on keys
echo "Using function: array_uintersect_assoc() \n ";
$result = array_intersect_uassoc($arr1
                $arr2, $arr3, "strcasecmp");
  
// printing result
print_r($result);
  
?>

chevron_right


Output:

Using function: array_uintersect_assoc() 
 Array
(
    [b] => GeeksforGeeks
)

Program: 2 Take two array (array1 and array2) and using user-defined key comparison function (uassoc_intersectFunction). Function return array with only mathched keys & values only.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Illusrate  array_intersect_uassoc() 
// Function in PHP 
  
  
function uassoc_intersectFunction($arr1, $arr2)
{
    if ($arr1 === $arr2) {
        return 0;
    }
    return ($arr1 > $arr2) ? 1 : -1;
}
  
// Code driven 
$arr1 = array(
    "0" => "Graph",
    "1" => "Dynamic",
    "3" => "Recursive",
    "4" => "Prime Factor"
);
$arr2 = array(
    "4" => "Prime",
    "2" => "Factorial",
    "3" => "Recursive",
    "7" => "Modulo"
);
  
$result = array_intersect_uassoc($arr1,
        $arr2, "uassoc_intersectFunction");
print_r($result);
  
?>

chevron_right


Output:

Array
(
    [3] => Recursive
)

Program: 3 Taking three array (arr1, arr2 and arr3) and using user-defined key comparison function (uassoc_intersectFunction). User-define function match as it is keys with same values but no any such case found then it will return NULL/Empty Array (In program 1 we used strcasecmp function this function applied on keys and ignore sensitiveness of case and return result GeeksforGeeks.)

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// illusrate array_intersect_uassoc() 
// Function in PHP 
  
  
function uassoc_intersectFunction($arr1, $arr2)
{
    if ($arr1 === $arr2) {
        return 0;
    }
    return ($arr1 > $arr2) ? 1 : -1;
}
  
// Code driven 
$arr1 = array(
    "a" => "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "a" => "Gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
  
// userdefine function match as it is keys
// with same values but no any such
// case so it will return NULL
$result = array_intersect_uassoc($arr1, $arr2, $arr3, "uassoc_intersectFunction");
print_r($result);
  
?>

chevron_right


Output:

Array
(
)

Below are some related array intersect PHP functions

  • PHP | array_intersect() Function: The function returns another array containing the elements of the first array that are present in all other arrays passed as the parameter. If no element matches then, a NULL array is returned.
  • PHP | array_intersect_assoc() Function: function returns all of the values of the first array that are present in all other arguments at the same index as that in first array, i.e., the keys are mainly used in the comparison.
  • PHP | array_uintersect_assoc() Function: compare the keys of an array and user-defined function to compare the values.
  • PHP | array_intersect_key() Function: The function returns another array containing the elements of the first array that are present in other arrays passed as the parameter whose key matches with each other. If no keys matches then, a NULL array is returned.v
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Program to illustrate==>
    // array_intersect() function
    // array_intersect_assoc() function
    // array_uintersect_assoc() function
    // array_intersect_uassoc() function
      
    $arr1 = array(
        "a" => "gfg",
        "b" => "GeeksforGeeks",
        "c" => "contribute"
    );
    $arr2 = array(
        "a" => "gfg",
        "B" => "GeeksforGeeks",
        "c" => "ide"
    );
    $arr3 = array(
        "a" => "Gfg",
        "B" => "GeeksforGeeks",
        "c" => "practice"
    );
      
    // The array_intersect() function compares
    // the values (not keys) of two (or more)
    // arrays, and returns the matches.
    echo "**********array_intersect********** \n ";
    $result = array_intersect($arr1, $arr2, $arr3);
    print_r($result);
      
      
    // array_intersect_assoc() returns an array 
    // containing all the values of arr1 that
    // are present in all the arguments. 
    // for above input it will return null
    echo "******array_intersect_assoc******** \n ";
    $result = array_intersect_assoc($arr1, $arr2, $arr3);
    print_r($result);
      
    // array_uintersect_assoc compare values (data) 
    // by using call back function
    echo "*********array_uintersect_assoc********** \n ";
    $result = array_uintersect_assoc($arr1,
                        $arr2, $arr3, "strcasecmp");
    print_r($result);
      
    // Compare the keys and values by using
    // a user-defined key comparison function
    // here callback function applicable on keys
    echo "*********array_uintersect_assoc *********\n ";
    $result = array_intersect_uassoc($arr1
                        $arr2, $arr3, "strcasecmp");
    print_r($result);
      
    // compute the intersection of two or more arrays.
    // function returns another array containing 
    // the elements of the first array that
    // are present in other arrays
    echo "*********array_intersect_key *********\n ";
    $result = array_intersect_key($arr1
                        $arr2, $arr3);
    print_r($result);
      
      
    ?>

    chevron_right

    
    

    Output:

    **********array_intersect********** 
     Array
    (
        [b] => GeeksforGeeks
    )
    ******array_intersect_assoc******** 
     Array
    (
    )
    *********array_uintersect_assoc********** 
     Array
    (
        [a] => gfg
    )
    *********array_uintersect_assoc *********
     Array
    (
        [b] => GeeksforGeeks
    )
    *********array_intersect_key *********
     Array
    (
        [a] => gfg
        [c] => contribute
    )
    



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.