PHP array_intersect_uassoc() Function (original) (raw)

Last Updated : 11 Jul, 2025

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 the condition true then return a TRUE value, else it returns a FALSE value.
Syntax:

array_intersect_uassoc($array1, array2,array2, 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 compare the keys of array by using case-insensitive strcasecmp function. It compares keys without case-sensitiveness.

php `

"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_intersect_uassoc() \n "; result=arrayintersectuassoc(result = array_intersect_uassoc(result=arrayintersectuassoc(arr1, arr2,arr2, arr2,arr3, "strcasecmp"); // printing result print_r($result); ?>

`

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 matched keys & values only.

PHP `

$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=arrayintersectuassoc(result = array_intersect_uassoc(result=arrayintersectuassoc(arr1, $arr2, "uassoc_intersectFunction"); print_r($result); ?>

`

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

PHP `

$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=arrayintersectuassoc(<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>r</mi><mi>r</mi><mn>1</mn><moseparator="true">,</mo></mrow><annotationencoding="application/x−tex">arr1,</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8389em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">rr</span><spanclass="mord">1</span><spanclass="mpunct">,</span></span></span></span>arr2,result = array_intersect_uassoc( <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>r</mi><mi>r</mi><mn>1</mn><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">arr1, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord">1</span><span class="mpunct">,</span></span></span></span>arr2, result=arrayintersectuassoc(<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>r</mi><mi>r</mi><mn>1</mn><moseparator="true">,</mo></mrow><annotationencoding="application/xtex">arr1,</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8389em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.02778em;">rr</span><spanclass="mord">1</span><spanclass="mpunct">,</span></span></span></span>arr2,arr3, "uassoc_intersectFunction"); print_r($result); ?>

`

Output:

Array ( )

Below are some related array intersect PHP functions

// array_intersect() function // array_intersect_assoc() function // array_uintersect_assoc() function // array_intersect_key() 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=arrayintersect(result = array_intersect(result=arrayintersect(arr1, arr2,arr2, 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=arrayintersectassoc(result = array_intersect_assoc(result=arrayintersectassoc(arr1, arr2,arr2, arr2,arr3); print_r($result); // array_uintersect_assoc compare values (data) // by using call back function echo "*********array_uintersect_assoc********** \n "; result=arrayuintersectassoc(result = array_uintersect_assoc(result=arrayuintersectassoc(arr1, arr2,arr2, 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=arrayintersectuassoc(result = array_intersect_uassoc(result=arrayintersectuassoc(arr1, arr2,arr2, 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=arrayintersectkey(result = array_intersect_key(result=arrayintersectkey(arr1, arr2,arr2, arr2,arr3); print_r($result); ?>

`

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][/c] => contribute )