PHP Function Reference

PHP array_uintersect_uassoc() Function



The PHP array_uintersect_uassoc() function compares keys and values of an array against one or more other arrays and returns all elements of the first array that are present in all the arguments. This function compares keys and values using two user-defined comparison functions.

Syntax

array_uintersect_uassoc(array, arrays, myfunc_value, myfunc_key)

Parameters

array Required. Specify an array to compare from.
arrays Required. Specify one or more arrays to compare against.
myfunc_value Required. Specify user-defined function to compare values. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
myfunc_key Required. Specify user-defined function to compare keys. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return Value

Returns an array containing all the entries from array which are present in all of the other arrays.

Exceptions

NA.

Example:

The example below shows the usage of array_uintersect_uassoc() function.

<?php
function MyFunc_Value($x, $y) {
  if ($x == $y) 
    return 0;
  return ($x < $y)? -1: 1;
}

function MyFunc_Key($x, $y) {
  if ($x == $y) 
    return 0;
  return ($x < $y)? -1: 1;
}

$Arr1 = array(0, 1, 2, 3);
$Arr2 = array(1, 0, 2, 3);

//comparing Arr1 against Arr2
$Arr1_intersect_Arr2 = array_uintersect_uassoc($Arr1, $Arr2, 
                           'MyFunc_Value', 'MyFunc_Key');
print_r($Arr1_intersect_Arr2);
?>

The output of the above code will be:

Array
(
    [2] => 2
    [3] => 3
)

Example:

Consider one more example which illustrates on usage of array_uintersect_uassoc() function.

<?php
function MyFunc_Value($x, $y) {
  if ($x == $y) 
    return 0;
  return ($x < $y)? -1: 1;
}

function MyFunc_Key($x, $y) {
  if ($x == $y) 
    return 0;
  return ($x < $y)? -1: 1;
}

$Arr1 = array("a"=>"Red", 
              "b"=>"Blue",
              "c"=>"Green");
$Arr2 = array("a"=>"Red", "Blue", "Green");

//comparing Arr1 against Arr2
$Arr1_intersect_Arr2 = array_uintersect_uassoc($Arr1, $Arr2, 
                           'MyFunc_Value', 'MyFunc_Key');
print_r($Arr1_intersect_Arr2);
?>

The output of the above code will be:

Array
(
    [a] => Red
)

❮ PHP Array Reference