PHP array_intersect_uassoc() function
The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing all the entries from array1 that are present in all the other arrays.
Syntax
array_intersect_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )
Parameter
array1(required)- This parameter signifies the input array to compare from.
array2(required)- This parameter signifies the input array to compare against.
array3….(optional)- It represents more arrays to compare against.
key_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Return
This function returns an array containing all the values in array1 that are present in all the other arrays.
Example 1
"green", "b" => "brown", "i" => "indigo", "red");
//initializing the input array1
$array2 = array("g" => "GREEN", "b" => "brown", "yellow", "red");
//return the intersection of arrays
print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
?>
Output
Array ( [b] => brown )
Example 2
$b)?1:-1;
}
$array1=array("1"=>"red","2"=>"green","3"=>"blue");
$array2=array("1"=>"red","5"=>"green","9"=>"blue");
$array3=array("1"=>"red","4"=>"green","7"=>"red");
// comparing the keys and values with additional index check
$intersection=array_intersect_uassoc ($array1,$array2,$array3,"myfunction");
//returns the intersection
print_r($intersection);
?>
Output
Array ( [1] => red )
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", 25=>"Reema", 30=>"Atul",70=>"Atul"); // comparing the keys and values with additional index check $intersection=array_intersect_uassoc($array1, $array2, "my_function"); //returning the intersection print_r($intersection); ?>
Output
Array ( [5] => Reema [30] => Atul )
