The array_diff_assoc() function compares the keys and values of two (or more) arrays, and returns the differences.
PHP array_diff_assoc() Function has the following syntax.
array_diff_assoc(array1,array2,array3...);
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | The array to compare from |
array2 | Required. | An array to compare against |
array3,... | Optional. | More arrays to compare against |
Compare the keys and values of two arrays, and return the differences:
<?php
$a1=array("R"=>"red","G"=>"green","B"=>"blue","Y"=>"yellow");
$a2=array("R"=>"red","G"=>"green");
$result=array_diff_assoc($a1,$a2);
print_r($result);
?>
The code above generates the following result.
Compare the keys and values of three arrays, and return the differences:
<?php
$a1=array("R"=>"red","G"=>"green","B"=>"blue","Y"=>"yellow");
$a2=array("R"=>"red","G"=>"green");
$a3=array("J"=>"java2s.com");
$result=array_diff_assoc($a1,$a2,$a3);
print_r($result);
?>
The code above generates the following result.