array_diff()
array_diff() — Computes the difference of arrays.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
Signature
array_diff(array $array, array ...$arrays): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $array | array | The array to compare from |
| ...$arrays | array | Arrays to compare against |
Return value
Returns an array containing all the entries from array that are not present in any of the other arrays. Keys in the array array are preserved.
Example
A runnable snippet:
$array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result);
Output:
Array ( [1] => blue )
Changelog
| Version | Change |
|---|---|
| 8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
See also
array_diff_assoc(), array_udiff(), array_intersect(), array_intersect_assoc()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_diff() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.