Current Location: Home> Latest Articles> Use array_udiff() to implement custom comparison logic

Use array_udiff() to implement custom comparison logic

M66 2025-06-06

In PHP, array_udiff() is a very powerful function that allows us to use a custom comparison function to compare differences between two or more arrays. This means that we can flexibly define "equal" or "unequal" judgment logic based on specific business needs, rather than relying on simple values ​​or key comparisons.

The basic syntax of array_udiff()

 array_udiff(array $array1, array $array2, callable $value_compare_func): array
  • $array1 : The first array to be compared.

  • $array2 : The second array that needs to be compared with the first array.

  • $value_compare_func : A user-defined comparison function.

The function returns the value in $array1 but not in $array2 and is compared with a user-defined function.

Practical application scenarios

Suppose we have two user lists, namely users who have logged in today and users who have been active in the past month. Each user information is an array containing id and email . We want to find out which "new users" are among the users logged in today, i.e. those that have never been active in the past.

Sample code

 // Users logged in today
$todayUsers = [
    ['id' => 101, 'email' => 'alice@m66.net'],
    ['id' => 102, 'email' => 'bob@m66.net'],
    ['id' => 103, 'email' => 'carol@m66.net'],
];

// Active users in the past month
$pastActiveUsers = [
    ['id' => 102, 'email' => 'bob@m66.net'],
    ['id' => 104, 'email' => 'dan@m66.net'],
];

// Custom comparison functions:pass email Determine whether the user is the same
function compareUsersByEmail($a, $b) {
    return strcmp($a['email'], $b['email']);
}

// Find out“New users”
$newUsers = array_udiff($todayUsers, $pastActiveUsers, 'compareUsersByEmail');

// Output result
echo "New users列表:\n";
foreach ($newUsers as $user) {
    echo "- {$user['email']} (ID: {$user['id']})\n";
}

Output result:

 New users列表:
- alice@m66.net (ID: 101)
- carol@m66.net (ID: 103)

Tips: Notes on customizing comparison functions

  • The comparison function in array_udiff() must return an integer :

    • Negative number: means that the first parameter is smaller than the second;

    • 0: means that two values ​​are equal;

    • Positive number: means that the first parameter is greater than the second.

  • In the custom comparison function, ensure a clear understanding of the structure of array elements and avoid accessing undefined keys.

Practical expansion: Multi-condition comparison

If we want to compare not only based on email, but also on id, we can write the comparison function like this:

 function compareUsersByIdAndEmail($a, $b) {
    $emailCompare = strcmp($a['email'], $b['email']);
    if ($emailCompare !== 0) {
        return $emailCompare;
    }
    return $a['id'] <=> $b['id'];
}

Using this function in array_udiff() can achieve more refined difference comparisons.

Summarize

array_udiff() provides a very flexible array difference set comparison method, especially suitable for processing scenarios of structured data. Through custom comparison functions, we can make judgments on arbitrary complex logic, greatly improving the freedom and expressiveness of array processing.

When processing complex business data such as user information, product lists, logging, etc., mastering the use of array_udiff() is undoubtedly an important skill.