When writing PHP applications, you may need to find entries whose key/value combinations are absent, using custom value comparison. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_udiff_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_udiff_assoc() in PHP?
Find entries whose key/value combinations are absent, using custom value comparison.
Its syntax is:
array_udiff_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.
Keys use normal built-in equality; only values use your callback. Return a three-way integer result.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$first = ["first" => "BLUE", "second" => "Red"];
$second = ["first" => "blue"];
print_r(array_udiff_assoc($first, $second, 'strcasecmp'));
Output:
Array
(
[second] => Red
)
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
$prices = ["pen" => 12, "book" => 30];
$oldPrices = ["pen" => 12, "book" => 22];
print_r(array_udiff_assoc($prices, $oldPrices, fn($left,$right) => $left <=> $right));
Output:
Array
(
[book] => 30
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Find changed named fields while comparing values with custom rules
Find changed named fields while comparing values with custom rules. Save the code in a PHP file and adapt the sample data to your project.
<?php
$originalProfile = ["nickname" => "MAYA", "city" => "London", "level" => "basic"];
$updatedProfile = ["nickname" => "maya", "city" => "Paris", "level" => "basic"];
$changedFields = array_udiff_assoc($updatedProfile, $originalProfile, "strcasecmp");
print_r($changedFields);
Output:
Array
(
[city] => Paris
)
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
Keys use normal built-in equality; only values use your callback. Return a three-way integer result.
- 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_udiff_assoc() | Find entries whose key/value combinations are absent, using custom value comparison |
| Related functions | array_udiff() ignores keys; array_udiff_uassoc() compares both keys and values with callbacks. |
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 | Keys use normal built-in equality; only values use your callback. Return a three-way integer result. |
Key Takeaways
array_udiff_assoc()helps you find entries whose key/value combinations are absent, using custom value comparison.- Keys use normal built-in equality; only values use your callback. Return a three-way integer result.
- array_udiff() ignores keys; array_udiff_uassoc() compares both keys and values with callbacks.
- Use the practical example as a starting point, but validate real application inputs.
array_udiff_assoc() is useful when you need to find entries whose key/value combinations are absent, using custom value comparison. Use its documented rules and the examples above to choose the right behavior for your data.