Skip to content
Related Articles

Related Articles

PHP | array_uintersect_assoc() Function

Improve Article
Save Article
Like Article
  • Last Updated : 30 Sep, 2021

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 defines the user-defined callback function and returns 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 value that contains the first array which is present in all other arrays. If there are no matches then it returns NULL.

Note: The array_uintersect_assoc() function compares the keys of an array and the 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"=>"maintenance ", "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).

PHP




<?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);
 
?>
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.

PHP




<?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" => "program",
    "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);
 
?>
Output: 
Array
(
)

 

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

PHP




<?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") );
 
?>
Output: 
Array
(
    [a] => gfg
)

 

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!