PHP | array_uintersect_assoc() Function

The array_uintersect_assoc() function is an inbuilt function in PHP used to compute the intersection of the array of keys for different values of two or more arrays. The initial array or first array compare to all others array by the callback function or user-defined function and returns the matches. The keys are used in the comparison unlike in array_uintersect() function.

Syntax:

array array_uintersect_assoc( $array1, $array2, $array3... array nth, arr_uintersect_Function)

Parameters: This function accepts many parameters as mentioned above and described below:



  • $array1: It is the initial array to compare with another array. It is mandatory parameter.
  • $array2: It is the second array compared with the first array key. It is also mandatory parameter.
  • $array3…: The arrays compared with the first array key. It is optional parameter.
  • arr_uintersect_Function: It is required parameter and used to hold the user defined function. It is a string that define the user-defined callback function and return an integer less than, equal to, or greater than zero if the first argument is less than, equal to, or greater than the second argument.

Return Value: It returns an array type values which contains the first array which is present in all others array. If there are no matches then it returns NULL.

Note: The array_uintersect_assoc() function compares the keys of an array and user-defined function compares the values.

Examples:

Input :
    $arr1 = array( "a"=>"Website", "b"=>"frontend", "c"=>"programmer" );
    $arr2 = array( "a"=>"Website", "b"=>"backend ", "c"=>"programmer" );
    $arr3 = array( "a"=>"Website", "b"=>"fullstack ", "c"=>"programmer" );
    $arr4 = array( "a"=>"Website", "b"=>"maintanance ", "c"=>"Team" );
Output :
    Array (
        [a] => Website
    )
Explanation: Only one element (website) is common in all arrays.


Input :
    $arr1 = array( "a"=>"Software", "b"=>"Testing", "c"=>"Tool" );
    $arr2 = array( "a"=>"Software", "b"=>"Testing ", "c"=>"Team" );
Output :
    Array (
        [a] => Software
        [b] => Testing
)
Explanation: Two values are common in both array = Software, and Testing.

Below programs illustrates the array_uintersect_assoc() function in PHP:

Program 1: This program uses two arrays (array1 and array2) and a user-defined key comparison function (arr_uintersect_Function).

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program for array_uintersect_assoc() function
function arr_uintersect_Function($a, $b) {
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
// Two array list with index and values
$arr1 = array (
    "a" => "Java",
    "b" => "Program",
    "c" => "Practice",
    "d" => "in",
    "f" => "Geeksforgeeks"
);
$arr2 = array (
    "a" => "Java",
    "b" => "Code ",
    "c" => "write",
    "d" => "in",
    "f" => "GeeksforgeeksIDE"
);
  
$result = array_uintersect_assoc($arr1, $arr2, "arr_uintersect_Function");
  
// Display result
print_r($result);
  
?>

chevron_right


Output:

Array
(
    [a] => Java
    [d] => in
)

Program 2: This program uses two arrays (array1 and array2) and a user-defined key comparison function (arr_uintersect_Function). If arrays do not match any key and values then it returns NULL.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program for array_uintersect_assoc() function
  
// User-defined function
function arr_uintersect_Function($a, $b) {
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
// Two array list with index and values
$arr1 = array (
    "a" => "my",
    "b" => "best",
    "c" => "programming",
    "d" => "blog",
    "e" => "Geeksforgeeks"
);
$arr2 = array (
    "f" => "My",
    "g" => "first ",
    "h" => "programm",
    "i" => "Geeks Hello"
);
$arr3 = array (
    "j" => "Analysis",
    "k" => "Algorithm ",
    "l" => "and",
    "m" => "Practice"
);
  
$result = array_uintersect_assoc( $arr1, $arr2, $arr3, "arr_uintersect_Function" );
  
// Display result
print_r($result);
  
?>

chevron_right


Output:

Array
(
)

Program 3: This program returns all the values of $arr1 that are present in all the arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$arr1 = array (
    "a" => "gfg",
    "b" => "ide",
    "c" => "runcode"
);
$arr2 = array (
    "a" => "GFG",
    "B" => "practice"
);
$arr3 = array (
    "a" => "Gfg",
    "B" => "contribute"
);
  
print_r( array_uintersect_assoc($arr1, $arr2, $arr3, "strcasecmp") );
  
?>

chevron_right


Output:

Array
(
    [a] => gfg
)

Reference: https://www.php.net/manual/en/function.array-uintersect-assoc.php



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.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.