Syntax:array_diff ($array1, $array2, $array3, ..., $arrayn)
Parameters : the function can take any number of arrays as parameters to be compared.Return type : this function compares the first array of parameters with the rest of the arrays and returns an array containing all the entries from $array1 that are not in any of the other arrays.Examples:Input: $array1 = (’a’,’ b’, ’c’); $array2 = (’a’,’ d’, ’e’); $array3 = (’a’,’ b’, ’f’); array_diff ($array1, $array2, $array3); Output: Array ([2] = > c) Input: $array1 = (’a’,’ b’, ’a’); $array2 = (’a’,’ d’, ’e’); Output: Array ([1] = > b)
The following program illustrates how array_diff() works in PHP:
// PHP code to illustrate how array_diff() works
function
Difference (
$array1
,
$array2
,
$array3
) {
return
(
array_diff
(
$array1
,
$array2
,
$array3
));
}
// Driver code
$array1
=
array
(
’a’
,
’b’
,
’ c’
,
’ d’
,
’e’
,
’f’
);
$array2
=
array
(
’a’
,
’b’
,
’ g’
,
’h’
);
$array3
=
array
(
’a’
,
’f’
,
’ i’
);
print_r (Difference (
$array1
,
$array2
,
$array3
));
?>
Output:Array ([ 2] = > c [3] = > d [4] = > e)
Important points to pay attention to : - It compares elements in their string representation. That is, 1 and 1 are equal for array_diff().
- The number of times an element is repeated in the first array does not matter. That is, if an element occurs 3 times in $array1 and only 1 time in other arrays, then all 3 occurrences of this element in the first array will be omitted from the output.
- For multidimensional arrays, we need to compare each of the dimensions in separately. For example: - $array1 [2], $array2 [2], etc.
Link : http://php.net/manual/en/function.array-diff.php