Current Location: Home> Latest Articles> Use array_diff_assoc() to filter out elements in the array with the same keys and values

Use array_diff_assoc() to filter out elements in the array with the same keys and values

M66 2025-05-17

In PHP, the array_diff_assoc() function is used to compare the keys and values ​​of two or more arrays, returning a new array containing the difference part. Specifically, it checks the differences between key-value pairs in the array, and if the keys and values ​​of both arrays are the same, the two elements are considered to be the same. Therefore, the array_diff_assoc() function can help us remove elements in an array with exactly the same key value.

This article will show you how to use the array_diff_assoc() function to remove elements with exactly the same key value in an array, and show some common application scenarios.

Function Syntax

 array_diff_assoc(array $array1, array $array2, array ...$arrays): array
  • $array1 : The first array.

  • $array2 : The second array.

  • $arrays : optional, multiple arrays can be passed in together.

This function returns an array containing key-value pairs that are in $array1 but not in $array2 .

Basic examples

Let's first show how to use the array_diff_assoc() function with a simple example.

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];

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

$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

Output result:

 Array
(
    [c] => 3
)

In the above example, array_diff_assoc() compares key-value pairs in array1 and array2 . Since a => 1 and b => 2 are the same in both arrays, they will not appear in the result array. Ultimately, only the key c => 3 is preserved.

Application scenario: Remove elements with exactly the same key value

Suppose you have an array containing multiple duplicate key-value pairs, and you want to remove elements with exactly the same key-value, which can be achieved by array_diff_assoc() .

 <?php
$array = [
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'a' => 1, // Repeat elements
    'b' => 2, // Repeat elements
];

$array_unique = array_diff_assoc($array, array());
print_r($array_unique);
?>

Output result:

 Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

In this example, the array_diff_assoc() function helps us remove duplicate key-value pairs from the original array, and only retains unique key-value pairs.

Things to note

  1. Both keys and values ​​must match : array_diff_assoc() will not only check whether the keys are the same, but also check whether the values ​​are the same. If the keys are the same but the values ​​are different, the elements will be considered different.

  2. Unlike array_diff() : array_diff() only compares values ​​and ignores keys. array_diff_assoc() compares both keys and values, so differences can be filtered more strictly.

  3. Multiple array comparison : multiple arrays can be passed in, array_diff_assoc() will return the differences in these arrays.

Conclusion

The array_diff_assoc() function is a very useful tool, especially when you need to compare arrays and remove duplicate elements. It can help us filter out the elements we want based on the exact match of keys and values. Mastering how to use this function can help you process data in arrays more efficiently.

If you have any questions about using other PHP functions, please continue to ask us!