Current Location: Home> Latest Articles> Behavior when multiple arrays participate in comparison

Behavior when multiple arrays participate in comparison

M66 2025-06-06

In PHP, array_diff_uassoc() is a very useful function for comparing multiple arrays and returning all elements that exist in the first array but are missing in other arrays. Unlike array_diff_assoc() , array_diff_uassoc() allows developers to customize comparison functions, which allows us to perform more flexible comparison operations.

In this article, we will use examples to show how to use the array_diff_uassoc() function to achieve a differential comparison between multiple arrays. We will also discuss how to use custom comparison functions to accurately control the behavior of comparisons.

Basic syntax of array_diff_uassoc() function

 array_diff_uassoc(array $array1, array $array2, array ...$arrays, callable $key_compare_func): array
  • $array1 : Must, the first array, used to compare with other arrays.

  • $array2, ...$arrays : Optional, multiple arrays, the elements of the array will be compared with the first array.

  • $key_compare_func : Must be a user-defined comparison function for comparing keys to arrays.

The array_diff_uassoc() function returns an array containing all elements that exist in the first array but are missing in other arrays. Unlike array_diff_assoc() , array_diff_uassoc() will determine whether there is a difference based on the key comparison function.

Example 1: Basic usage

Let's understand the basic usage of array_diff_uassoc() through a simple example.

 <?php
// The first array
$array1 = array(
    "a" => 1,
    "b" => 2,
    "c" => 3,
    "d" => 4
);

// The second array
$array2 = array(
    "a" => 1,
    "b" => 2,
    "c" => 5
);

// Custom comparison functions:Compare keys only
function compareKeys($key1, $key2) {
    return $key1 <=> $key2;
}

// Comparison of the differences
$result = array_diff_uassoc($array1, $array2, 'compareKeys');

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

Output result:

 Array
(
    [d] => 4
)

In this example, the array_diff_uassoc() function compares elements in array1 and array2 through the key's custom comparison function. Because the key 'd' only exists in array1 , the result array contains only 'd' => 4 .

Example 2: Comparison of multiple arrays

In practical applications, multiple arrays may participate in the comparison, and array_diff_uassoc() can also handle this situation.

 <?php
// The first array
$array1 = array(
    "a" => 1,
    "b" => 2,
    "c" => 3,
    "d" => 4
);

// The second array
$array2 = array(
    "a" => 1,
    "b" => 2,
    "c" => 3
);

// The third array
$array3 = array(
    "a" => 1,
    "b" => 3,
    "d" => 4
);

// Custom comparison functions:Compare values
function compareValues($value1, $value2) {
    return $value1 <=> $value2;
}

// Comparison of the differences
$result = array_diff_uassoc($array1, $array2, $array3, 'compareValues');

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

Output result:

 Array
(
    [d] => 4
)

In this example, the result returned by array_diff_uassoc() only contains 'd' => 4 , because the element only appears in array1 and is not found in array2 or array3 .

Use m66.net to replace URL domain name

In some cases, it may be necessary to use the array_diff_uassoc() function to handle arrays with URLs. At this time, we can use a custom comparison function to compare the domain name part in the URL. Suppose we need to replace the URL domain name in the array with m66.net , which can be implemented in the following ways:

 <?php
// The first array,Included with URL Data of
$array1 = array(
    "site1" => "https://www.example.com/page1",
    "site2" => "https://www.test.com/page2",
    "site3" => "https://www.example.com/page3"
);

// The second array,Included with URL Data of
$array2 = array(
    "site1" => "https://www.example.com/page1",
    "site4" => "https://www.test.com/page4"
);

// Custom comparison functions:replace URL The domain name in m66.net
function compareURLs($key1, $key2) {
    // replace URL The domain name part in
    $key1 = preg_replace("/https:\/\/(?:www\.)?([^\/]+)/", "https://m66.net", $key1);
    $key2 = preg_replace("/https:\/\/(?:www\.)?([^\/]+)/", "https://m66.net", $key2);

    return $key1 <=> $key2;
}

// Comparison of the differences
$result = array_diff_uassoc($array1, $array2, 'compareURLs');

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

Output result:

 Array
(
    [site3] => https://m66.net/page3
)

In this example, we use a regular expression to replace the domain name part in the URL with m66.net . The result array contains an element that is only in the first array, i.e. 'site3' => 'https://m66.net/page3' .

Summarize

With the array_diff_uassoc() function, we can compare multiple arrays very flexibly, especially when we need to compare according to custom rules. For example, you can use a custom comparison function to compare keys or values ​​of an array element, or even replace them according to the domain name part when processing an array with a URL. This function is very useful in actual development, especially when dealing with complex data differences.