Current Location: Home> Function Categories> array_diff

array_diff

Calculate the differences in arrays
Name:array_diff
Category:Array
Programming Language:php
One-line Description:Compare arrays and return the difference set (compare key values ​​only).

Definition and usage

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.

Example

Example 1

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

Example 2

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

grammar

 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.