PHP's array_diff Function
The array_diff function in PHP is a built-in function that is used to compare arrays and return the values that are present in one array but not in another.
The array_diff function in PHP is a built-in function that compares arrays and returns the values present in the first array but not in any of the others. It is useful whenever you need the set difference between lists — for example, comparing data from a database against values submitted through a form, or finding which items were removed between two snapshots of a list.
This chapter covers how array_diff works, its syntax, how it handles keys and value comparison, and common gotchas. It is part of the PHP arrays topic.
How array_diff Works
The array_diff function compares two or more arrays and returns a new array containing the values that are present in the first array but in none of the subsequent arrays. Two points are essential to understand:
- Comparison is value-based; keys are ignored. Only the values are matched, but the keys from the first array are preserved in the result (they are not reindexed).
- Values are compared as strings. Internally PHP treats two elements as equal when
(string) $a === (string) $b. So the integer1and the string"1"are considered the same value.
Syntax
The syntax for the array_diff function is as follows:
PHP array_diff function syntax
array array_diff (array $array1, array $array2 [, array $... ]);The array1 parameter is the first array — the one whose values you want to keep. The array2 parameter (and any further arrays) are the arrays to compare against. The function returns the values from array1 that are not found in any of the other arrays.
Examples
Let's take a look at a few examples of how you can use the array_diff function in your PHP code.
PHP example of array_diff function usage
This code will output the following:
Array ( [0] => 1 )As you can see, the array_diff function has returned the value 1, which is present in $array1 but not in $array2. Note that the original key (0) from the first array is preserved.
PHP example with string values (case sensitivity)
This code will output the following:
Array ( [1] => Banana )In this example, string comparison is case-sensitive. Since 'Banana' does not exactly match 'banana', it is returned as a difference. The key 1 (Banana's original position) is kept in the result.
Comparing against multiple arrays
You can pass more than two arrays. A value is kept only if it is missing from every other array:
<?php
$array1 = [1, 2, 3, 4, 5];
$array2 = [2, 4];
$array3 = [5];
$result = array_diff($array1, $array2, $array3);
print_r($result);
?>Output:
Array ( [0] => 1 [2] => 3 )Values 2 and 4 are removed by $array2, and 5 is removed by $array3, leaving 1 and 3 with their original keys.
Reindexing the result
Because the original keys are preserved, the result can have gaps in its numeric keys. If you need a clean, sequentially indexed list, wrap the call in array_values():
<?php
$result = array_values(array_diff([1, 2, 3], [2]));
print_r($result);
?>Output:
Array ( [0] => 1 [1] => 3 )Gotcha: loose, string-based comparison
Because elements are compared as strings, mixed types can match unexpectedly:
<?php
$result = array_diff(['1', '2', 3], [1, 2]);
print_r($result);
?>Output:
Array ( [2] => 3 )Here '1' matches 1 and '2' matches 2 (string comparison), so only 3 remains. Objects and arrays as values trigger a warning, since they cannot be cast to a string cleanly.
Related functions
array_diff only compares values. PHP offers variants for different needs:
array_diff_key()— compares keys instead of values.array_diff_assoc()— compares both keys and values.array_intersect()— the opposite: returns values present in all arrays.array_merge()— combines arrays rather than subtracting them.
Conclusion
The array_diff function is a useful tool for comparing arrays and finding the differences between them. Whether you're working with data from a database, or simply comparing lists of values, the array_diff function can help you quickly and easily find the values that are unique to one array. By using this function in your PHP code, you can streamline your development process and make it easier to find and process the data that you need.