PHP Array Difference

Among the huge list of PHP array functions, we are going to see about some of them used in finding differences between given input arrays. By using PHP array difference functions, we can find differences between arrays with respect to their keys or values. Some basic PHP array difference functions are,

  • array_diff
  • array_diff_key
  • array_diff_assoc

Apart from the above list of functions, array differences can also be found, based on the index check by using the custom made functions, used as a callback, while invoking appropriate inbuilt PHP array difference functions. And, the functions are,

  • array_diff_ukey
  • array_diff_uassoc


We can further list out another set of PHP functions that are used in finding differences between given input arrays by comparing their entries with the use of user-defined callback functions. These are,

  • array_udiff
  • array_udiff_assoc
  • array_udiff_uassoc

Let us see about each of these functions in detail.

Finding PHP Array Differences without Callback

Now, we are going to gain more information on the list of array difference functions listed at first. These functions are used to compare PHP arrays given as its arguments, with respect to the array keys, values or values with associated index.


It accepts at least two arrays, but, maximum in any number of arrays, as its arguments, thereby, the elements from the first array will be compared with the rest of the array elements.

If any of the element in the first array is not exist in the remaining set of arrays, then it will be returned as one of the elements of the resultant array. Similarly, the entire array will be compared with other arrays.


This function is used as similar as array_diff, but for comparing array elements with respect to their key. Thereby, if any key of the first array is not matched with the remaining set of input parameter arrays, then, it will be added into the resultant array.


This function is used to compare the values between given arrays, by considering the key index associated with these values. So, even if the values are matched and their associated index or not same, these would also be considered as array differences.

We can get more clear on seeing the following simple example that deals with the above three functions.

$array_from = array("fruit1" => "apple", "fruit2" => "grapes","friut3" =>"banana");
$array_against = array("fruit1" => "orange", "fruit2" => "grapes","friut4" => "banana");
$value_differences = array_diff($array_from, $array_against);
$key_differences = array_diff_key($array_from, $array_against);
$assoc_index_differences = array_diff_assoc($array_from, $array_against);
print "<PRE>";
print "</PRE>";

In the above PHP program, when we compare input arrays using the various array functions the expected results are displayed to the browser using PHP print statements, as shown below.

[fruit1] => apple
[friut3] => banana
[fruit1] => apple
[friut3] => banana

On using array_diff, the value that exists in $array_from, and not exists in another array, [fruit]=>apple is returned as the resultant array as expected. Similarly, the other PHP array difference functions return the different element of the array as shown above.

PHP Array Difference Functions with Callback

In this type of finding array differences, user-defined functions are used as a callback. For that, we need to specify the name of that callback function as an argument of appropriate PHP inbuilt function used for returning array differences.

We can categorize these array functions depends on, for what reason they are using user defined functions as a callback. Since we have listed them already, let as have a glance at the following notes specifying the purpose of these functions.

  • array_diff_ukey , array_diff_uassoc are used where the developer required additional constraints to be made upon the key and the index associated with array values, respectively.
  • array_udiff , array_udiff_assoc functions are used for comparing array data, using some custom made functions.
  • Added to that, one more PHP function named as array_udiff_uassoc is used to perform both array data comparison and array key index comparison, by using user-defined functions referred as a callback.

This PHP code tutorial was published on July 18, 2013.

↑ Back to Top