PHP array_diff_assoc() Function
Description
The array_diff_assoc() function compares the keys and values of two (or more) arrays, and returns the differences.
Syntax
PHP array_diff_assoc() Function has the following syntax.
array_diff_assoc(array1,array2,array3...);
Parameter
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 |
Example 1
Compare the keys and values of two arrays, and return the differences:
<?php//from w w w . j av a 2 s. com
$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.
Example 2
Compare the keys and values of three arrays, and return the differences:
<?php//w ww .ja v a2 s . com
$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.