php editor Xigua will give you a detailed analysis of how PHP calculates the difference in arrays. In actual development, comparing the differences between two arrays is a common requirement, which can help us find out newly added, deleted and modified elements. We can easily implement this function through PHP's built-in functions and some simple logic. Next, let us explore how to use PHP to calculate the difference of arrays to make your development work more efficient and convenient.
PHP Guide to Calculating Array Differences
Calculating array differences is the process of comparing and contrasting the differences of elements in two or more arrays. php provides several ways to accomplish this task, depending on the desired result and the structure of the array.
array_diff() function
array_diff()
The function is used to find elements that are present in the first array and not present in all other arrays. Its syntax is as follows:
array_diff($array1, $array2, ..., $arrayN);
For example:
$array1 = ["apple", "banana", "cherry"]; $array2 = ["banana", "orange"]; $diff = array_diff($array1, $array2); // ["apple", "cherry"]
array_diff_assoc() function
array_diff_assoc()
Function is similar to array_diff()
, but it also compares array keys. It returns elements with the same key and value. Its syntax is as follows:
array_diff_assoc($array1, $array2, ..., $arrayN);
For example:
$array1 = ["apple" => 1, "banana" => 2, "cherry" => 3]; $array2 = ["banana" => 2, "orange" => 4]; $diff = array_diff_assoc($array1, $array2); // ["apple" => 1, "cherry" => 3]
array_diff_key() function
array_diff_key()
The function is used to find elements with different keys in two arrays. Its syntax is as follows:
array_diff_key($array1, $array2, ..., $arrayN);
For example:
$array1 = ["apple" => 1, "banana" => 2]; $array2 = ["banana" => 2, "orange" => 4]; $diff = array_diff_key($array1, $array2); // ["apple" => 1]
array_udiff() and array_uintersect() functions
Thearray_udiff()
and array_uintersect()
functions are used to compare elements in two arrays using a user-defined comparison function. array_udiff()
returns elements that are present in the first array and not present in all other arrays, while array_uintersect()
returns elements that are present in both arrays. Their syntax is as follows:
array_udiff($array1, $array2, ..., $arrayN, $compareFunction); array_uintersect($array1, $array2, ..., $arrayN, $compareFunction);
Custom comparison functions must take two parameters (array elements) and return an integer:
For example, the following custom comparison function will sort an array of fruits by name:
function compareFruits($a, $b) { return strcmp($a["name"], $b["name"]); }
Calculate the difference using a custom comparison function:
$array1 = [ ["name" => "apple", "price" => 1], ["name" => "banana", "price" => 2], ["name" => "cherry", "price" => 3], ]; $array2 = [ ["name" => "banana", "price" => 2], ["name" => "orange", "price" => 4], ]; $diff = array_udiff($array1, $array2, "compareFruits"); // [["name" => "apple", "price" => 1], ["name" => "cherry", "price" => 3]]
In the above example, the compareFruits
function sorts the array elements by name, so the result contains fruits that are present in array1
and not present in array2
.
The above is the detailed content of How to calculate difference of array in PHP. For more information, please follow other related articles on the PHP Chinese website!