Home >>PHP Array Functions >PHP array_diff_assoc() Function

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function is used to get the difference between one or more given arrays. It compares two arrays according to their keys and values and returns the elements that are present in the first array but not in the other input arrays. It can take any number of arrays as parameters needed to be compared.

Syntax:

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

Parameter Values

Parameter Description
array1 This is a required parameter. This parameter defines the array to compare from.
array2 This is a required parameter. This parameter definesan array to compare against.
array3 This is an optional parameter. This parameter defines more arrays to compare against.

Here is an example of array_diff_assoc() function in PHP:

<html>
<body>
<pre>
<?php
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4");
$y=array("a"=>"1","b"=>"2","c"=>"3");
$result=array_diff_assoc($x,$y);
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [d] => 4
)

Example 2:

<html>
<body>
<pre>
<?php
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5","f"=>"6");
$y=array("a"=>"1","b"=>"2","c"=>"3");
$z=array("b"=>"3","c"=>"4","e"=>"4","f"=>"6");
$result=array_diff_assoc($x,$y,$z);  // keys and value both should match
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [d] => 4
    [e] => 5
)

No Sidebar ads