How to calculate difference of array in PHP

王林
Release: 2024-03-19 11:42:01
forward
967 people have browsed it

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);
Copy after login

For example:

$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "orange"];

$diff = array_diff($array1, $array2); // ["apple", "cherry"]
Copy after login

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);
Copy after login

For example:

$array1 = ["apple" => 1, "banana" => 2, "cherry" => 3];
$array2 = ["banana" => 2, "orange" => 4];

$diff = array_diff_assoc($array1, $array2); // ["apple" => 1, "cherry" => 3]
Copy after login

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);
Copy after login

For example:

$array1 = ["apple" => 1, "banana" => 2];
$array2 = ["banana" => 2, "orange" => 4];

$diff = array_diff_key($array1, $array2); // ["apple" => 1]
Copy after login

array_udiff() and array_uintersect() functions

The

array_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);
Copy after login

Custom comparison functions must take two parameters (array elements) and return an integer:

  • If the first element is less than the second element, return -1.
  • If the first element is equal to the second element, return 0.
  • If the first element is greater than the second element, return 1.

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"]);
}
Copy after login

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]]
Copy after login

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!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template