Current Location: Home> Latest Articles> How to implement array symmetric difference sets using multiple array_diff()?

How to implement array symmetric difference sets using multiple array_diff()?

M66 2025-06-06

In daily development, we often encounter situations where we need to compare two arrays and find out the different elements between them. At this time, if you just find the elements that exist in an array but not in another array, the array_diff() function is enough. But if we need to find out elements that do not exist in two arrays (that is, symmetric difference sets) , we need to combine array_diff() slightly.

What is a symmetric difference set?

Symmetrical difference sets, in layman's terms, are a collection of non-repetitive elements in two sets. For example:

 $array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];

The symmetric difference set of these two arrays is [1, 2, 5, 6] because these four numbers exist in one of the arrays and do not exist in the other.

Implemented with PHP array_diff()

array_diff() can find values ​​that exist in the first array but not in other arrays. Therefore, we can use two array_diff() to get the symmetric difference set:

 <?php
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];

// Find out array1 There is array2 Nothing in
$diff1 = array_diff($array1, $array2);

// Find out array2 There is array1 Nothing in
$diff2 = array_diff($array2, $array1);

// Merge the results of two differential sets
$symmetricDifference = array_merge($diff1, $diff2);

print_r($symmetricDifference);
?>

The output will be:

 Array
(
    [0] => 1
    [1] => 2
    [4] => 5
    [5] => 6
)

The keys here may not be continuous. If you need to re-index, you can add a sentence:

 $symmetricDifference = array_values($symmetricDifference);

Application in actual projects

Suppose you are building a membership system and you need to compare the differences in permissions between the two user groups. Different permissions can be found in this way:

 <?php
$userGroupA = ['read', 'write', 'delete'];
$userGroupB = ['read', 'export', 'import'];

$diff1 = array_diff($userGroupA, $userGroupB);
$diff2 = array_diff($userGroupB, $userGroupA);

$permissionDiff = array_merge($diff1, $diff2);
$permissionDiff = array_values($permissionDiff);

print_r($permissionDiff);

// result:['write', 'delete', 'export', 'import']
?>

Tip: Use array_unique() to further deduplicate

Although we merge two mutually exclusive differences in the symmetric difference set, if there are duplicate elements in the array, duplicate results may also be produced. You can add array_unique() to deduplicate:

 $symmetricDifference = array_unique(array_merge($diff1, $diff2));

Summarize

By combining two array_diff() functions, we can easily get the symmetric difference set of two arrays. This method is both clear and concise, and is especially suitable for permission systems, data comparison and other scenarios.

You can encapsulate this method into a function to improve reusability:

 function symmetric_diff(array $a, array $b): array {
    return array_values(array_unique(array_merge(
        array_diff($a, $b),
        array_diff($b, $a)
    )));
}

// Sample call
$result = symmetric_diff([1, 2, 3], [2, 3, 4]);
print_r($result); // Output:[1, 4]

Hope this tip can help you in your PHP project!