w3resource

PHP: array_diff_assoc() function

PHP: Computes the difference of arrays with additional index check

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.

Version:

(PHP 4 and above)

Syntax:

array_diff_assoc(ref_array, array1, array2,.....)

Parameter:

Name Description Required /
Optional
Type
ref_array The first array which will be compared with other arrays. Required Array
array1 Compared with the first array. Required Array
array2 Compared with the first array. Optional Array

Return value

A new array containing the keys and values of ref_array that are not present in any of the other arrays i.e. array1, array2 etc.

Value Type: Array

Example:

<?php
$array1 = array('Math'=>77, 20, 'Geography'=>89,30,'Language'=>67);
$array2 = array('Math'=>77, 'science'=>91 ,20, 'History'=>71);
$diff_result = array_diff_assoc($array1, $array2);
print_r($diff_result);
?>

Output :

Array ( [Geography] => 89 [1] => 30 [Language] => 67)

Pictorial Presentation:

php function reference: array_diff_assoc() function

View the example in the browser

Practice here online:

See also

PHP Function Reference

Previous:array_count_values
Next: array_diff_key



Follow us on Facebook and Twitter for latest update.