PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function

The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input array that are present in all the other given arrays.

Syntax

array_uintersect_uassoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func , callable $key_compare_func )

Parameter

array1(required)- This array represents the first input array.

array2(required)- This array represents the second input array.

array3….(optional)- It represents more arrays to compare against.

value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function returns an integer greater (<), equal to (=), or less than ( >) than 0 if the first argument is greater(<), equal to(=), or less than( >) than the second argument.

key_compare_func(required)- This parameter represents the key comparison callback function.

Return

This function returns an array containing all the values of the first input array that are present in all the other given arrays.

Example 1

 "Reema", "b" => "bEENA", "c" => "Babita", "Ria");
 echo("Array 1:\n");
 print_r($array1);
 //initializing the input array2
 $array2 = array("a" => "REEMA", "b" => "beena", "Rahul", "Ria"); 
 echo("\nArray 2:\n");
 print_r($array2);
 //Computing the intersection of arrays with key and value functions
 $uintersect_uassoc=array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp");
 echo("Intersection Array:\n");
 print_r($uintersect_uassoc);
 ?> 

Output

Array 1:
 Array
 (
     [a] => Reema
     [b] => bEENA
     [c] => Babita
     [0] => Ria
 ) 
 Array 2:
 Array
 (
     [a] => REEMA
     [b] => beena
     [0] => Rahul
     [1] => Ria 
 )
 Intersection Array:
 Array
 (
     [a] => Reema
     [b] => bEENA
 ) 

Example 2

$val2)?1:-1; 
 }
 //input array1
 $array1=array("a"=>"PHP","b"=>"C#","c"=>"C++","d"=>"Python","e"=>"AI");
 //input array2
 $array2=array("a"=>"PHP","b"=>"C#","c"=>"Python");
 //input array2
 $array3=array("a"=>"PHP","c"=>"Java","d"=>"AI","e"=>"C++");
 // returns the intersection of the three given arrays with the help of callable functions 
 $intersection_uassoc=array_uintersect_uassoc($array1,$array2,$array3,"myfunction","myfunction");
 //will return PHP
 print_r($intersection_uassoc);
 ?> 

Output

Array
 (
     [a] => PHP
 ) 

Example 3

$b)? 1 : 0; 
 } 
 // initializing the Input Array 1 
 $array1 = array(05=>"Reema", 10=>"raj", 30=>"Atul"); 
 // initializing the Input Array 2
 $array2 = array(05=>"Reema", 40=>"Smitha", 30=>"Atul");  
 //returns the intersection of the three given arrays with the help of value callable functions
 $intersection=array_uintersect_uassoc ($array1, $array2, "myfunction","myfunction"); 
 //will return intersection Atul
 print_r($intersection); 
 ?> 

Output

Array
 (
     [30] => Atul
 )