When writing PHP applications, you may need to keep matches using separately customizable key and value comparisons. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_uintersect_uassoc() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is array_uintersect_uassoc() in PHP?
Keep matches using separately customizable key and value comparisons.
Its syntax is:
array_uintersect_uassoc(array $array, array ...$arrays, callable $value_compare_func, callable $key_compare_func): array
Parameters used by this operation:
$array— The collection being examined or updated.$arrays— The collection being examined or updated.$value_compare_func— A callable that controls the comparison or transformation; callbacks should follow the expected return contract.$key_compare_func— A callable that controls the comparison or transformation; callbacks should follow the expected return contract.
The function expects the value callback before the key callback; both require consistent integer comparisons.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$first = ["Theme" => "DARK", "Language" => "EN"];
$second = ["theme" => "dark"];
print_r(array_uintersect_uassoc($first, $second, 'strcasecmp', 'strcasecmp'));
Output:
Array
(
[Theme] => DARK
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$first = ["ID" => 15, "MODE" => 2];
$second = ["id" => 15];
print_r(array_uintersect_uassoc($first, $second, fn($a,$b) => $a <=> $b, 'strcasecmp'));
Output:
Array
(
[ID] => 15
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Match fields from two feeds when their key casing varies
Match fields from two feeds when their key casing varies. Save the code in a PHP file and adapt the sample data to your project.
<?php
$leftRecord = ["CustomerID" => "A-10", "Country" => "UK"];
$rightRecord = ["customerid" => "a-10", "country" => "US"];
$matching = array_uintersect_uassoc($leftRecord, $rightRecord, "strcasecmp", "strcasecmp");
print_r($matching);
Output:
Array
(
[CustomerID] => A-10
)
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
The function expects the value callback before the key callback; both require consistent integer comparisons.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
array_uintersect_uassoc() | Keep matches using separately customizable key and value comparisons |
| Related functions | array_udiff_uassoc() keeps differences; array_uintersect_assoc() uses built-in key comparison. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Returning true or false from a comparator | Return a negative integer, zero, or a positive integer to express ordering. |
| Mixing up key and value callbacks | Pay attention to the comparator order and which part of each entry it compares. |
| Assuming input values are rewritten | The function expects the value callback before the key callback; both require consistent integer comparisons. |
Key Takeaways
array_uintersect_uassoc()helps you keep matches using separately customizable key and value comparisons.- The function expects the value callback before the key callback; both require consistent integer comparisons.
- array_udiff_uassoc() keeps differences; array_uintersect_assoc() uses built-in key comparison.
- Use the practical example as a starting point, but validate real application inputs.
array_uintersect_uassoc() is useful when you need to keep matches using separately customizable key and value comparisons. Use its documented rules and the examples above to choose the right behavior for your data.