Open In App

PHP array_uintersect() Function

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.




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


Output:

Array
(
    [d] => raj
)

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




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


Output:

Array
(
    [1] => geeks
)

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads