Tutorialspoint.dev

PHP | array_uintersect_assoc() Function

The array_uintersect_assoc() function is an inbuilt function in PHP used to compute the intersection of the keys and 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() .

Syntax:

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

Parameters Used: This function accepts two parameters and both parameters are mandatory and the others are optional The parameters are described below:

  1. $array1 :
    It is the initial array and compare to another array.
    It’s Mandatory.
  2. $array2 :
    The array Compared with the first array key.
    It’s Mandatory.
  3. $array3 :
    The second array Compared with the first array key.
    Its Optional.
  4. arr_uintersectFunction :
    Its Required user defined function and A string that define 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:
Returns an array types values which contain the first array which is present in all others array. If there are no matches then array return NULL.



Note:
This is built-in function (array_uintersect_assoc()) to compare the keys of an array and user-defined function to compare the values.

Example 1 :

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 comman in all array.

Example 2 :

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 = Sofware, and Testing.

Let’s take simple example to understand about array_uintersect_assoc() Function.

Program: 1
Take two array (array1 and array2) and using user-defined key comparison function (arr_uintersectFunction).

<?php
//PHP code  for array_uintersect_assoc function
function arr_uintersectFunction($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_uintersectFunction");
  
//print result.
print_r($result);
?>

Output:

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

Program: 2
Take two array (array1 and array2) and using user-defined key comparison function (arr_uintersectFunction). which has not match any key and values then array retrun NULL.

<?php
//PHP code  for array_uintersect_assoc function
  
//User-defined function
function arr_uintersectFunction($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_uintersectFunction");
//print result.
print_r($result);
?>

Output:

Array
(
)

Program: 3 Returns all the values of $arr1 that are present in all the arguments.

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


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter