PHP array_diff() Function
Last Updated : 17 Mar 2025
The array_diff() function compares two or more arrays and returns an array with the keys and values from the first array, only if the value is not present in any of the other arrays. This function was introduced in PHP 4.0.
Syntax
| Parameter | Description | Is compulsory |
|---|---|---|
| array1 | The array to compare from | compulsory |
| array2 | An array to compare against | compulsory |
| array3 | More arrays to compare against | Optional |
Return Type
It returns an array containing the entries from array1 that are not present in any of the other arrays.
Important note- Only the value is used in comparison
Example 1
Output:
Array ( [0] => netbeans )
Example 2
Output:
Array ( [4] => zend )
Example 3
Output:
Array ( [2] => c [3] => d [4] => e )
Example 4
Output:
Array ( [3] => yellow )
Difference between array_diff( ) , array_diff_key( ) and array_diff_assoc( )
Output:
Array( [fruit1] => apple ) Array ( [friut3] => mango ) Array ( [fruit1] => apple [friut3] => mango )
On using array_diff, the value that exists in $array1, and not exists in another array, is returned as the resultant array. Similarly, the other PHP array difference functions return the different element of the array.
Next TopicPhp-array-diff-key-function