PHP array_diff() Function

The array_diff() function compares the values of two (or more) arrays, and returns the differences.

You can diff several arrays by providing more parameters. The function will return an array of values in the first array that do not appear in the second and subsequent arrays.

ParameterDescription
array1Required. The specified array which will be compared with other arrays.
array2Required. Compared with the first array.
array3,…Optional. Compared with the first array.

Syntax

array_diff(array1,array2,array3...);

Example

$array1=array("a"=>"Prince","b"=>"James","c"=>"Samuel","d"=>"Cooper");
$array2=array("e"=>"Prince","f"=>"James","g"=>"Samuel");
$result=array_diff($array1,$array2);
print_r($result);

Output

Array ( [d] => Cooper ) 

Leave a Reply

Your email address will not be published. Required fields are marked *