PHP Tutorial - PHP array_diff_assoc() Function






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

ParameterIs RequiredDescription
array1Required.The array to compare from
array2Required.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
$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
$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.