array_diff
Calculate the differences in arrays
array_diff()
function returns an array of differences between two arrays. This array includes all key values in the array being compared but not in any other parameter array.
In the returned array, the key name remains the same.
Compare the key 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 ( $a1 , $a2 ) ; print_r ( $result ) ; ?>
Try it yourself
Compare the values of three arrays and return the difference:
<?php $a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" , "d" => "yellow" ) ; $a2 = array ( "e" => "red" , "f" => "black" , "g" => "purple" ) ; $a3 = array ( "a" => "red" , "b" => "black" , "h" => "yellow" ) ; $result = array_diff ( $a1 , $a2 , $a3 ) ; print_r ( $result ) ; ?>
Try it yourself
array_diff ( 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. |