Wednesday, July 3, 2024
HomeLanguagesPhpPHP array_uintersect() Function

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"=>"neveropen", "d"=>"raj")
        $a2=array("d"=>"articles", "e"=>"raj", "f"=>"coding")

Output :
Array
(
    [d] => raj
)

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

Output :
Array
(
    [1] => neveropen
)

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"=>"neveropen", "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"=>"neveropen", "2"=>"for", "3"=>"geek",
                                    "4"=>"coding");
$a2 = array("1"=>"neveropen", "2"=>"for", "3"=>"php",
                        "4"=>"coding", "5"=>"ide");
$a3 = array("6"=>"cpp", "7"=>"java", 8=>"neveropen");
  
$result=array_uintersect($a1, $a2, $a3, "user_function");
print_r($result);
?>


Output:

Array
(
    [1] => neveropen
)

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

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments