PHP array_diff_assoc() Function

The array_diff_assoc() function is used to compare an array against another array and returns the difference. Unlike array_diff() the array keys are also used in the comparison.

This function compares the keys and values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

ParameterDescription
array1The first array which will be compared with other arrays.
array2Compared with the first array.
array3,…Compared with the first array.

Syntax

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

Example

$array1=array("a"=>"Prince","b"=>"James","c"=>"Samuel","d"=>"Cooper");
$array2=array("a"=>"Prince","b"=>"James","c"=>"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 *