array_diff() Function in PHP

Comparing two or more array values ​​in PHP is very easy with the help of a particular function in PHP. When there is a need to compare the elements of an array or its values, you need to write code to manually check each element of both arrays that you are comparing, or You can use some predefined function provided by the language. In this tutorial, you will learn how to use such a predefined array comparison function of PHP.

PHP array_diff() Function

The array_diff() is a built-in function of PHP, and this function is implemented for calculating or comparing the difference between two or more arrays. The comparison between arrays is made based on the values of the element between one or more array, and then their differences are returned as a new array. This predefined function can be implemented from PHP version 4.0.1 and onwards.

The syntax for using the array_diff() function is:

Syntax:
array_diff ( array $arr1 , array $arr2, ... )

This function can accept any number of parameterized arrays (depending on the memory of the system) for comparison.

Here is a code snippet of how to use it:

Example:
<?php

$arr1 = array("x" => "gold", "silver", "diamond", "platinum");
$arr2 = array("y" => "gold", "silver", "diamond");

$res = array_diff($arr1, $arr2);

print_r($res);

?>
Program Output:
Array
(
    [2] => platinum
)

The program can also be written like this:

<?php

$arr1=array("x"=>"gold","y"=>"silver","z"=>"diamond","w"=>"platinum");
$arr2=array("x"=>"gold","y"=>"silver","z"=>"diamond");

$res=array_diff($arr1,$arr2);

print_r($res);

?>
Program Output:
Array
(
    [w] => platinum
)

How the array_diff() Function and Its Parameters Works

Since this function can take multiple parameters without any limit, the first parameterized array (array1) is a mandatory parameter with which other arrays will be compared. The second parameterized array (array2) is the one against which the comparison will be performed. If there are more optional arrays (in parameters), then these arrays will be compared against the first parameter.

Once this function is executed, it will return an array containing all the entries from the first array that are not present in any other arrays with which it is compared.

Characteristics of PHP array_diff() Function

  • This function will compare the elements in the form of a string representation. It means, '1' and 1 will be treated equally within this array_diff() function.
  • When there is a multi-dimensional array, you have to compare each of the array dimensions separately.
  • In case there is any duplication or reappearance of any element in the first array, it will not make any difference. This means, if an element is found three times in the first array and only once in another array(s), then all three of the elements of these elements will be omitted, and only one will be considered.

Another Example of the PHP array_diff () Function Using Multiple Arrays

Example:
<?php

$arr1 = array("a" => "gold", "silver", "white", "red", "purple", "orange", "black");
$arr2 = array("b" => "gold", "yellow", "red","black");
$arr3 = array("c" => "pink", "white", "diamond");
print_r( array_diff( $arr1, $arr2, $arr3) );

?>
Program Output:
Array
(
    [0] => silver
    [3] => purple
    [4] => orange
)

Scroll Back to Top