Current Location: Home> Latest Articles> Performance comparison: array_diff() vs array_diff_assoc()

Performance comparison: array_diff() vs array_diff_assoc()

M66 2025-05-13

In PHP, array_diff() and array_diff_assoc() are two commonly used array comparison functions. They are often used to find the differences between two arrays, but they behave differently. This article will discuss in detail the differences between the two functions and their performance differences.

1. How array_diff() works

The array_diff() function is used to compare two arrays and return all elements in the first array that are not in the second array. It compares the values ​​of two arrays, and ignores the keys of the array.

grammar:

 array_diff(array $array1, array $array2, array ...$arrays): array

Example:

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

$result = array_diff($array1, $array2);
print_r($result);  // Output: [0 => 1, 3 => 4]

In this example, array_diff() compares the values ​​of $array1 and $array2 , returning elements in $array1 that are not in $array2 .

2. How array_diff_assoc() works

The array_diff_assoc() function is also used to compare two arrays, but unlike array_diff() , it not only compares values, but also compares key names. Therefore, only elements that do not match values ​​and keys are considered different.

grammar:

 array_diff_assoc(array $array1, array $array2, array ...$arrays): array

Example:

 $array1 = [1 => 'a', 2 => 'b', 3 => 'c'];
$array2 = [1 => 'a', 2 => 'x'];

$result = array_diff_assoc($array1, $array2);
print_r($result);  // Output: [3 => 'c']

In this example, array_diff_assoc() compares the keys and values ​​of $array1 and $array2 . It considers that element c with key 3 does not exist in the second array, so it returns that element.

3. Performance comparison: array_diff() and array_diff_assoc()

array_diff() and array_diff_assoc() are both functions used to find array differences, but due to their different working principles, they also differ in performance.

  • array_diff() compares only values, which makes it usually more efficient when operating, especially when arrays are large, because its comparison process involves only values ​​and does not require checking keys.

  • array_diff_assoc() not only needs to compare values, but also keys, so it does more when comparing, which may cause it to perform slightly worse, especially when used in large arrays.

4. Which one is more efficient?

If you only care about the values ​​of the array and don't care about the differences in keys, using array_diff() is more efficient. If you need to consider both key and value differences, array_diff_assoc() is essential, but it will have a greater performance overhead.

5. Summary

  • Comparing values ​​using array_diff() is more efficient and is suitable for cases where only array values ​​need to be considered.

  • Using array_diff_assoc() to compare keys and values ​​is more precise, but it is slightly inferior in performance, and is suitable for scenarios where both keys and values ​​need to match.

If you only care about the difference in values ​​in your application, choosing array_diff() will bring better performance. If both keys and values ​​need to be strictly matched, array_diff_assoc() is a more suitable choice.