Home >>PHP Array Functions >PHP array_diff() Function
PHP array_diff() Function is used to calculate the difference between two or more given arrays. Itfindsthe difference according to the values of the elements, between one or more array and return the differences in the form of a new array. It returns all the entries that are present in the first array and are not present in any other given arrays. It can take any number of arrays as parameters needed to be compared.
Syntax:
array_diff($array1, $array2, $array3, ...);
Parameter | Description |
---|---|
array1 | This is a required parameter. This parameter definesthe array to compare from. |
array2 | This is a required parameter. This parameter definesan array to compare against. |
array3 | This is an optional parameter. This parameter definesmore arrays to compare against. |
Here is an example of array_diff() function in PHP:
<html> <body> <pre> <?php $x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5","f"=>"6"); $y=array("e"=>"1","f"=>"2","g"=>"3"); $result=array_diff($x,$y); print_r($result); ?> </pre> </body> </html>
Array ( [d] => 4 [e] => 5 [f] => 6 )
Example 2:
<html> <body> <?php $x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5","f"=>"6"); $y=array("e"=>"1","f"=>"2","g"=>"3"); $z=array("h"=>"3","i"=>"4","k"=>"5","l"=>"6","m"=>"7"); $result=array_diff($x,$y,$z); // it will return a empty array print_r($result); ?> </body> </html>
Array ( )