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 to compare each of the dimensions separately. For example:- $array1,$array2 etc.
- How to get the function name inside a function in PHP ?
- PHP | Ds\Set first() Function
- p5.js | hue() function
- p5.js | red() function
- PHP Ds\Set get() Function
- PHP | exp() Function
- D3.js | d3.hcl() Function
- p5.js | min() function
- p5.js | nfp() Function
- D3.js | d3.set.add() Function
- p5.js | nfs() Function
- PHP Ds\Set sum() Function
- PHP | dir() 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.