PHP | array_uintersect() Function

The array_uintersect() is an inbuilt function in PHP and is used to compute the intersection of two or more arrays depending on the values. The first array values are compared with all the other arrays with the help of an user-defined function and the matches are returned.

Syntax:

array_uintersect($array1, $array2, $array3, ..... $arrayn, user_function

Parameters: This function accepts two types of parameters. One is a list of arrays and another one is a user-defined function.

  • List of arrays: This function accepts a list of arrays separated by spaces for which we want to find the intersection. In the above syntax the list of arrays is $array1, $array2, $array3, ….. $arrayn. It can accept any number of arrays separated by spaces with the minimum being 2.
  • user_function: This is a string type parameter which is the name of a user defined function. The function returns 0 when the values in its parameter are same, returns 1 if first parameter is greater than the second, else it returns -1.

Return Value: The function returns another array containing all of 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.

Examples:



Input : $a1=array("a"=>"striver", "b"=>"geeks", "d"=>"raj")
        $a2=array("d"=>"articles", "e"=>"raj", "f"=>"coding")

Output :
Array
(
    [d] => raj
)

Input :$a1 = array("1"=>"geeks", "2"=>"for", "3"=>"geek", "4"=>"coding")
$a2 = array("1"=>"geeks", "2"=>"for", "3"=>"php", "4"=>"coding", "5"=>"ide")
$a3 = array("6"=>"cpp", "7"=>"java", 8=>"geeks")

Output :
Array
(
    [1] => geeks
)

Below programs illustrate the array_uintersect() function:

Program 1: PHP program to demonstrate the working of array_uintersect() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the working of 
// array_uintersect() function  
  
// user-defined function
function user_function($a, $b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
  
// arrays 
$a1=array("a"=>"striver", "b"=>"geeks", "d"=>"raj");
$a2=array("d"=>"articles", "e"=>"raj", "f"=>"coding");
  
$result=array_uintersect($a1, $a2, "user_function");
print_r($result);
?>

chevron_right


Output:

Array
(
    [d] => raj
)

Program 2: PHP program to demonstrate the working of array_uintersect() function with three arrays.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the working of 
// array_uintersect() function with 3 arrays 
  
// user-defined function
function user_function($a, $b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
  
// 3 arrays 
$a1 = array("1"=>"geeks", "2"=>"for", "3"=>"geek",
                                    "4"=>"coding");
$a2 = array("1"=>"geeks", "2"=>"for", "3"=>"php",
                        "4"=>"coding", "5"=>"ide");
$a3 = array("6"=>"cpp", "7"=>"java", 8=>"geeks");
  
$result=array_uintersect($a1, $a2, $a3, "user_function");
print_r($result);
?>

chevron_right


Output:

Array
(
    [1] => geeks
)

Reference:
http://php.net/manual/en/function.array-uintersect.php



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.




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.