PHP Array Difference

Among 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 in-built PHP array difference functions. And, the functions are,

  • array_diff_ukey
  • array_diff_uassoc

php_array_differences

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.

array_diff

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

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

array_diff_key

This function is used as similar as array_diff, but for comparing array elements with respect to their key. There by, 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.

array_diff_assoc

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.

<?php
$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_r($value_differences);
print_r($key_differences);
print_r($assoc_index_differences);
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.

Array
(
[fruit1] => apple
)
Array
(
[friut3] => banana
)
Array
(
[fruit1] => apple
[friut3] => banana
)

On using array_diff, the value that exist in $array_from, and not exist in other 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 in-built function used for returning array differences.

We can categorize these array functions depends on, for what reason they are using user defined functions as callback. Since, we have listed them already, let as have a glance with 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 callback.

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

↑ Back to Top