array_diff_assoc
Calculate the difference set of arrays with index check
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.).
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
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
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
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. |