PHP array_uintersect_uassoc() function

Example

Compare the key names and values of two arrays (using user-defined functions for comparison) and return the intersection (matching):

<?php
function myfunction_key($a, $b)
{
if ($a === $b)
  {
  return 0;
  }
  return ($a > $b) ? 1 : -1;
}
function myfunction_value($a, $b)
{
if ($a === $b)
  {
  return 0;
  }
  return ($a > $b) ? 1 : -1;
}
$a1 = array("a" => "red", "b" => "green", "c" => "blue");
$a2 = array("a" => "red", "b" => "green", "c" => "green");
$result=array_uintersect_uassoc($a1, $a2, "myfunction_key", "myfunction_value");
print_r($result);
?>

Run Example

Definition and Usage

The array_uintersect_uassoc() function compares the key names and values of two (or more) arrays and returns the intersection.

Note:This function uses two user-defined functions for comparison; the first function compares key names, and the second function compares values!

This function compares the key names and values of two (or more) arrays and returns an intersection array, which includes all elements present in the compared arrays (array1etc.), as well as any other parameter arrays (array2 or array3 etc.) key names and values.

Attention, with array_uintersect() The difference is that the key name also needs to be compared. Both the key value and the key name (index) are compared by the callback function.

Syntax

array_uintersect_uassoc(array1,array2,array3...myfunction_key,myfunction_value)
Parameters Description
array1 Required. The first array to be compared with other arrays.
array2 Required. The array to be compared with the first array.
array3,... Optional. Other arrays to be compared with the first array.
myfunction_key

Required. The name of the user-defined function used to compare array key names.

Define a callable comparison function. If the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0.

myfunction_value

Required. The name of the user-defined function used to compare array key values.

Define a callable comparison function. If the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0.

Use a user-defined callback function myfunction_key and myfunction_value to calculate the intersection of two or more arrays (i.e., array1 that exist in the array and also exist in any other array), and return the resulting array.

Both key names and key values are compared simultaneously, such as "a"=>1 and "b"=>1, these two elements are not equal.

myfunction_key The specified function is used to compare whether the key names are equal.myfunction_value The specified function is used to compare whether the key values are equal. Both functions have two parameters to be compared. If the first parameter is less than the second parameter, the function returns a negative number; if the two parameters are equal, 0 should be returned; if the first parameter is greater than the second, a positive number is returned.

The array returned maintains the unchanged key names.

Technical Details

Return Value: Returns an array containing all elements that exist in array1 Also in all other arrays at the same time.
PHP Version: 5+