Current Location: Home> Latest Articles> How to use array_diff_uassoc() function? What are its basic syntax and practical application examples?

How to use array_diff_uassoc() function? What are its basic syntax and practical application examples?

M66 2025-06-06

In PHP, array_diff_uassoc() is a function that compares arrays, which can compare two or more arrays and return the difference part based on custom key-value comparison rules. This function is very useful in scenarios where complex data structures or comparisons are required based on keys.

1. Basic syntax

 array_diff_uassoc(array $array1, array $array2, callable $key_compare_func): array

Parameter description :

  • array1 : The first array that needs to be compared.

  • array2 : The second array that needs to be compared, which can be multiple arrays.

  • key_compare_func : A callback function that compares keys to an array and must return an integer. The return value determines the order of key values: if you return a negative number, it means that the first key is smaller than the second key; if you return a zero, it means that the two keys are equal; if you return a positive number, it means that the first key is larger than the second key.

Return value : This function returns an array containing different elements, that is, elements that exist in the first array but not in the second array, and the comparison is based on custom key-value rules.

2. Practical application examples

Suppose we have two arrays, $array1 and $array2 , we want to find out the parts of the two arrays with different keys.

 <?php
// Define two arrays
$array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3
];

$array2 = [
    "a" => 1,
    "b" => 2,
    "d" => 4
];

// Custom key comparison function
function custom_key_compare($key1, $key2) {
    return strcmp($key1, $key2);  // use strcmp Comparison key
}

// use array_diff_uassoc Compare arrays
$result = array_diff_uassoc($array1, $array2, 'custom_key_compare');

// Output result
print_r($result);
?>

Output :

 Array
(
    [c] => 3
)

In this example, we define two arrays $array1 and $array2 . $array1 contains the keys "a" , "b" , and "c" , while $array2 contains the keys "a" , "b" , and "d" . When using the array_diff_uassoc() function, the custom_key_compare() function is used to compare keys of an array. In this example, the value 3 corresponding to the key "c" is returned because it exists in $array1 , but cannot be found in $array2 .

3. Things to note

  • The array_diff_uassoc() function not only compares values, but also compares based on custom rules of keys. If you do not need a custom key comparison rule, you can use array_diff_assoc() .

  • If the callback function compares keys in an unreasonable way, it may lead to incorrect results, especially when there are many or complex array elements.

  • This function does not change the original array, it returns a new array.

4. Use scenarios

array_diff_uassoc() is commonly used in the following scenarios:

  • Compare two arrays with the same key but different values.

  • When you need to compare rules based on custom keys, find out the difference between the two arrays.

  • Used for data processing, analysis or deduplication, especially when complex key-value pairs are involved.

5. Combined with actual URL examples

If we need to involve URL operations in our code and want to replace the domain name in the URL with m66.net , we can refer to the following code:

 <?php
// Define a containing URL Array of
$urls = [
    "https://www.example.com/page1",
    "https://www.example.com/page2",
    "https://www.example.com/page3"
];

// use array_map and regular replacement URL Domain name in
$updatedUrls = array_map(function($url) {
    return preg_replace('/https:\/\/www\..+\.com/', 'https://m66.net', $url);
}, $urls);

// Output the result after replacement
print_r($updatedUrls);
?>

Output :