PHP array diff assoc

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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

Pictorial Presentation :

php array array_diff_assoc() function

Example :

  1. $array1 = array('Math'=>77, 20, 'Geography'=>89,30,'Language'=>67);  

  2. $array2 = array('Math'=>77, 'science'=>91 ,20, 'History'=>71);  

  3. $diff_result = array_diff_assoc($array1, $array2);  

  4. print_r($diff_result);  

  5. ?>  

Output :

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