The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns all the entries that are present in the first array which are not present in any other arrays.
array_diff($array1, $array2, $array3, ...,$arrayn)
Parameters: The function can take any number of arrays as parameters needed to be compared.
Return Type: This function compares the first array in parameters with rest of the arrays and returns an array containing all the entries from $array1 that are not present in any of the other arrays.
Input : $array1 = ('a', 'b', 'c'); $array2 = ('a', 'd', 'e'); $array3 = ('a', 'b', 'f'); array_diff($array1, $array2, $array3); Output : Array (  => c ) Input : $array1 = ('a', 'b', 'a'); $array2 = ('a', 'd', 'e'); Output : Array (  => b )
Below program illustrates the working of array_diff() in PHP:
Array (  => c  => d  => e )
Important points to note:
- It compares the elements in their string representation. That is, 1 and ‘1’ are both equal for array_diff().
- The number of repetition of element in first array doesn’t matter. That is if an element occurs 3 times in $array1 and only 1 time in other arrays then all of the 3 occurrences of that element in first array will be omitted in output.
- For multi-dimensional arrays, we need to compare each of the dimensions separately. For example:- $array1,$array2 etc.
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai