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,
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,
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,
Let us see about each of these functions in detail.
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.
<?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 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.
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.
This PHP code tutorial was published on July 18, 2013.