When writing PHP applications, you may need to keep matching key/value pairs while comparing values with a callback. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_uintersect_assoc() 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_assoc() in PHP?
Keep matching key/value pairs while comparing values with a callback.
Its syntax is:
array_uintersect_assoc(array $array, array ...$arrays, callable $value_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 comparison is built in and the callback compares values only. Original keys and values are preserved.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$left = ["color" => "BLUE", "size" => "M"];
$right = ["color" => "blue", "size" => "L"];
print_r(array_uintersect_assoc($left, $right, 'strcasecmp'));
Output:
Array
(
[color] => BLUE
)
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
$left = ["a" => 10, "b" => 20];
$right = ["b" => 20];
print_r(array_uintersect_assoc($left, $right, fn($a,$b) => $a <=> $b));
Output:
Array
(
[b] => 20
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Keep matching configuration fields while treating case differences in values as equal
Keep matching configuration fields while treating case differences in values as equal. Save the code in a PHP file and adapt the sample data to your project.
<?php
$submitted = ["language" => "EN", "timezone" => "UTC", "theme" => "dark"];
$allowedChoices = ["language" => "en", "theme" => "light"];
$unchanged = array_uintersect_assoc($submitted, $allowedChoices, "strcasecmp");
print_r($unchanged);
Output:
Array
(
[language] => EN
)
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
Key comparison is built in and the callback compares values only. Original keys and values are preserved.
- 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_assoc() | Keep matching key/value pairs while comparing values with a callback |
| Related functions | array_uintersect() ignores keys; array_uintersect_uassoc() also customizes 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 | Key comparison is built in and the callback compares values only. Original keys and values are preserved. |
Key Takeaways
array_uintersect_assoc()helps you keep matching key/value pairs while comparing values with a callback.- Key comparison is built in and the callback compares values only. Original keys and values are preserved.
- array_uintersect() ignores keys; array_uintersect_uassoc() also customizes key comparison.
- Use the practical example as a starting point, but validate real application inputs.
array_uintersect_assoc() is useful when you need to keep matching key/value pairs while comparing values with a callback. Use its documented rules and the examples above to choose the right behavior for your data.