Current Location: Home> Function Categories> array_diff_assoc

array_diff_assoc

Calculate the difference set of arrays with index check
Name:array_diff_assoc
Category:Array
Programming Language:php
One-line Description:Compare arrays and return the difference set (compare key names and key values).

Definition and usage

The array_diff_assoc() function is used to compare the key names and key values ​​of two (or more) arrays and return the difference set.

The function compares the key names and key values ​​of two (or more) arrays and returns an array of differences that include all key names and key values ​​in the array being compared ( array1 ) but not in any other parameter array ( array2 or array3 , etc.).

Example

Example 1

Compare the keys and values ​​of two arrays and return the difference:

 <?php
$a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" , ​​"d" => "yellow" ) ;
$a2 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ;

$result = array_diff_assoc ( $a1 , $a2 ) ;
print_r ( $result ) ;
?>

Try it yourself

Example 2

Compare the keys and values ​​of two arrays and return the difference:

 <?php
$a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" , ​​"d" => "yellow" ) ;
$a2 = array ( "e" => "red" , "f" => "green" , "g" => "blue" ) ;

$result = array_diff_assoc ( $a1 , $a2 ) ;
print_r ( $result ) ;
?>

Try it yourself

Example 3

Compare the keys and values ​​of three arrays and return the difference:

 <?php
$a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" , ​​"d" => "yellow" ) ;
$a2 = array ( "a" => "red" , "f" => "green" , "g" => "blue" ) ;
$a3 = array ( "h" => "red" , "b" => "green" , "g" => "blue" ) ;

$result = array_diff_assoc ( $a1 , $a2 , $a3 ) ;
print_r ( $result ) ;
?>

Try it yourself

grammar

 array_diff_assoc ( array1 , array2 , array3 ... ) ;
parameter describe
array1 Required. The first array that is compared with other arrays.
array2 Required. The array that compares to the first array.
array3 ,... Optional. Other arrays that are compared with the first array.