How do you use the array_diff function in PHP?

The array_diff function is used to calculate the difference between arrays, returning elements that exist in the first array but are not present in the other arrays.

The syntax of this function is as follows:
array_diff(array1, array2, array3, …)

array1 is the array to be compared, while array2, array3, and others are other arrays to be compared.

The return value is a new array that contains elements that exist in array1 but not in any other arrays.

For example, let’s say we have two arrays:
$array1 = array(“apple”, “banana”, “orange”);
$array2 = array(“apple”, “banana”, “grape”);

Compare using the array_diff function:
$result = array_diff($array1, $array2);

The outcome will be:
array(“orange”, “grape”)

The elements “orange” and “grape” exist in $array1 but not in $array2.

bannerAds