W3docs

Mastering the PHP array_uintersect_uassoc Function

Learn how to use PHP array_uintersect_uassoc to compute array intersections with custom value and key comparison callbacks, with examples.

PHP is a powerful language with various functions, and array_uintersect_uassoc is one of the most versatile functions in the PHP array functions library. This function allows you to compute the intersection of arrays with additional user-defined key comparison functions.

If you're looking for a way to compare two arrays and get the values that exist in all of them, then the array_uintersect_uassoc function is what you need. This function provides the flexibility to compare arrays with custom comparison functions, which makes it a valuable tool for any PHP developer.

Here's a detailed overview of the array_uintersect_uassoc function, along with some examples to help you understand how it works.

What is the array_uintersect_uassoc Function?

The array_uintersect_uassoc function is a PHP built-in function used to compute the intersection of arrays using user-defined comparison functions for data and keys. This function compares the values of two arrays and returns the values that exist in all of them, provided their keys also match according to the key comparison function.

One of the key features of the array_uintersect_uassoc function is its ability to compare arrays using user-defined key and value comparison functions. This means that you can customize the comparison logic to suit your specific needs.

How to Use the array_uintersect_uassoc Function

The array_uintersect_uassoc function accepts two or more arrays followed by two callbacks — the last argument is always the key-comparison callback, and the one before it is the value-comparison callback. Here's the syntax:

PHP array_uintersect_uassoc function syntax

array_uintersect_uassoc(
    array $array1,
    array $array2,
    array ...$arrays,
    callable $value_compare_func,
    callable $key_compare_func
): array

Parameters:

  • array1: The base array. Values that survive the comparison are taken from here, with their original keys preserved.
  • array2, ...$arrays: One or more arrays to compare against array1.
  • value_compare_func: A callback for comparing values. It receives two values and must return an integer less than, equal to, or greater than 0 — exactly like a sort comparator. Returning 0 means "equal".
  • key_compare_func: A callback for comparing keys, with the same three-way return contract.

Return Value: Returns an array containing every entry of array1 whose value and key both match an entry in each of the other arrays. An element survives only when both callbacks return 0 against some element in every other array.

Because both callbacks follow the standard <=> (spaceship) contract, returning 1 or -1 simply tells PHP the elements are not equal — only 0 counts as a match.

Let's take a look at an example of how to use the array_uintersect_uassoc function to compare two arrays:

PHP Use the array_uintersect_uassoc function to compare two arrays

<?php

function compare_data($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}

function compare_keys($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}

$array1 = ["a" => "green", "b" => "brown", "c" => "blue", "d" => "red"];
$array2 = ["a" => "green", "b" => "yellow", "blue", "d" => "red"];

$result = array_uintersect_uassoc($array1, $array2, "compare_data", "compare_keys");
print_r($result);

?>

In the example above, we first define two custom comparison functions for data and keys, and then we define two arrays that we want to compare. We then call the array_uintersect_uassoc function with the two arrays and the two comparison functions as arguments.

The array_uintersect_uassoc function returns an array that contains the values that exist in both arrays. In this example, the result would be:

Array
(
    [a] => green
    [d] => red
)

Why only a and d? An element survives only when both its key and its value match.

  • [a] => green matches: key a and value green exist in both arrays. Kept.
  • [b] => brown is dropped: key b exists in both, but the values (brown vs yellow) differ.
  • [c] => blue is dropped: array1 has it under key c, but array2 stores blue under the auto-assigned numeric key 0, so the keys don't match.
  • [d] => red matches: key d and value red exist in both. Kept.

That last case is exactly what separates this function from value-only intersection: even when a value is present in both arrays, a mismatched key removes it.

Note: The example uses named functions for broad compatibility, but modern PHP (7.4+) supports concise arrow functions (e.g., fn($a, $b) => $a <=> $b). Choose the syntax that aligns with your project's PHP version requirements.

When to reach for this function

Use array_uintersect_uassoc when all three of these are true: you need an intersection, the keys are meaningful (associative data), and either the values or the keys need custom comparison logic (case-insensitive strings, objects, locale-aware ordering, tolerance on floats, etc.). If you only need one of those, a simpler relative is a better fit:

See the PHP Arrays chapter for a broader overview of working with arrays.

A Real-World Example: Case-Insensitive Matching

The first example used strict comparators, so it behaves like the built-in array_intersect_assoc. The real power appears when the callbacks do something the built-ins cannot — here, matching keys and values regardless of case:

<?php

$inventory = ["Apple" => "RED", "Banana" => "yellow", "Cherry" => "dark-red"];
$catalog   = ["apple" => "red", "banana" => "GREEN", "cherry" => "DARK-RED"];

$result = array_uintersect_uassoc(
    $inventory,
    $catalog,
    fn($a, $b) => strcasecmp($a, $b), // compare values, ignoring case
    fn($a, $b) => strcasecmp($a, $b)  // compare keys, ignoring case
);

print_r($result);

?>

strcasecmp returns 0 when two strings are equal ignoring case, so it slots straight into the comparator contract. The result keeps entries from $inventory whose key and value match $catalog case-insensitively:

Array
(
    [Apple] => RED
    [Cherry] => dark-red
)

Banana is excluded because, although the keys match, the values (yellow vs GREEN) differ even ignoring case. A plain array_intersect_assoc would have matched none of these, because its built-in comparison is case-sensitive.

Benefits of Using the array_uintersect_uassoc Function

There are several benefits to using the array_uintersect_uassoc function in your PHP projects. Here are some of the key benefits of using this function:

1. Precise Key-Value Matching

Unlike array_uintersect, this function ensures that only elements with matching keys and values are returned. This prevents false positives when comparing associative arrays where keys carry semantic meaning.

2. Custom Comparison Logic

You can define precise equality rules for both values and keys, handling complex data types or custom sorting requirements that built-in operators cannot cover.

3. Native Performance

Implemented in C within PHP's core, it executes efficiently for standard array operations without external dependencies, making it suitable for moderately sized datasets.

Tips for Using the array_uintersect_uassoc Function

Here are some tips for using the array_uintersect_uassoc function in your PHP projects:

1. Define Custom Comparison Functions

To get the most out of the array_uintersect_uassoc function, it's essential to define custom comparison functions. This will allow you to customize the comparison logic to fit the specific needs of your project.

2. Use Type-Safe Comparison

When defining your custom comparison functions, it's important to use type-safe comparison operators. This will ensure that the comparison is done on the correct data types, which will improve the accuracy of the results.

3. Test Your Comparison Functions

Before using your custom comparison functions in production, it's important to test them thoroughly. This will help you identify any bugs or issues and ensure that your comparison logic is working correctly.

Conclusion

In this article, we've explored the array_uintersect_uassoc function in PHP. This function is a powerful tool for comparing arrays and finding the values that exist in all of them. By using custom comparison functions for data and keys, you can customize the comparison logic to fit the specific needs of your project.

If you're looking to take your PHP skills to the next level, mastering the array_uintersect_uassoc function is a great place to start. With its flexibility and versatility, this function is an essential tool for any PHP developer.

Thank you for reading our guide to the array_uintersect_uassoc function. We hope you found it useful and informative. If you have any questions or feedback, please feel free to contact us.

Practice

Practice
What is the purpose of array_uintersect_uassoc function in PHP?
What is the purpose of array_uintersect_uassoc function in PHP?
Was this page helpful?