Open In App

PHP array_uintersect_uassoc() Function

The array_uintersect_uassoc() function is an inbuilt function in PHP which is used to computes the intersection of two arrays. There is the role of a callback function which helps in comparing and computing the indexes values, It compares the keys. It also compares the values inside the two or more arrays using two user-defined functions and then returns the matches. The array_uintersect_uassoc() returns an array containing all the values of the first array that present in all the arguments. For comparisons, the keys are used in the first function and that value is used in the second one.

Syntax:



array array_uintersect_uassoc( $array1, $array2, $array3..., $function_key, $function_value )

Parameters: This function accepts multiple parameters as mentioned above and described below:

Return Value: It returns an array containing all the values of array1 that are present in all the arguments.



Below programs illustrate the array_uintersect_uassoc() Function in PHP:

Program 1:




<?php
$arr1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$arr2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
  
print_r(array_uintersect_uassoc($arr1, $arr2, "strcasecmp", "strcasecmp"));
?>

Output:

Array
(
    [a] => green
    [b] => brown
)

Program 2:




<?php
function function_key($a, $b)
{
    if ($a == $b)
        return 0;
      
    return ($a > $b) ? 1 : -1;
}
  
function function_value($a, $b)
{
    if ($a == $b)
        return 0;
          
    return ($a > $b) ? 1 : -1;
}
  
$arr1=array("1"=>"Geeks","2"=>"GeeksforGeeks","3"=>"Geeks1");
$arr2=array("1"=>"Geeks","2"=>"GFG","3"=>"Geeks1");
  
$res = array_uintersect_uassoc($arr1, $arr2, "function_key", "function_value");
  
print_r($res);
?>

Output:

Array 
( 
    [1] => Geeks 
    [3] => Geeks1 
)

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


Article Tags :