How to find the difference between two arrays in php

zbt
Release: 2023-08-04 17:04:46
Original
2021 people have browsed it

php method to find the difference between two arrays: 1. Use the array_diff() function. This function accepts two or more arrays as parameters and returns the values ​​that appear in the first array but in other arrays. Non-existent elements; 2. Use a loop and the in_array() function to traverse the elements of the first array one by one, and use the in_array() function to check whether the element exists in the second array; 3. Use the array_diff_key() function; 4. Use the array_udiff() function, etc.

How to find the difference between two arrays in php

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

In PHP, we often need to process and compare arrays. One common operation is to find the difference between two arrays, i.e. find elements that appear in one array but not in the other. This article will introduce several methods to achieve this operation.

Method 1: Use the array_diff() function

The easiest way is to use PHP’s built-in array_diff() function. This function accepts two or more arrays as arguments and returns elements that appear in the first array but are not present in the other arrays.

The sample code is as follows:

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$result = array_diff($array1, $array2);
print_r($result);
Copy after login

The output result is:

Array
(
[0] => 1
[1] => 2
)
Copy after login
Copy after login
Copy after login

This shows that the elements that appear in $array1 but do not exist in $array2 are 1 and 2.

Method 2: Use loops and in_array() function

In addition to the array_diff() function, we can also use loops and in_array() functions to find two Array difference. This method iterates through the elements of the first array one by one and checks whether the element is present in the second array using the in_array() function.

The sample code is as follows:

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$result = [];
foreach ($array1 as $element) {
if (!in_array($element, $array2)) {
$result[] = $element;
}
}
print_r($result);
Copy after login

The output result is:

Array
(
[0] => 1
[1] => 2
)
Copy after login
Copy after login
Copy after login

Method three:

Another method is to use array_diff_key ()function. This function accepts two or more arrays as arguments and returns key values ​​that exist in the first array but not in the other arrays.

The sample code is as follows:

$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['c' => 3, 'd' => 4, 'e' => 5];
$result = array_diff_key($array1, $array2);
print_r($result);
Copy after login

The output result is:

Array
(
[a] => 1
[b] => 2
)
Copy after login

This shows that the key values ​​that exist in $array1 but do not exist in $array2 are 'a' and 'b'.

Method 4: Use the array_udiff() function

The last method is to use the array_udiff() function. This function requires an additional user-defined comparison function to be passed in to compare the elements of the array.

The sample code is as follows:

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$result = array_udiff($array1, $array2, function($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
});
print_r($result);
Copy after login

The output result is:

Array
(
[0] => 1
[1] => 2
)
Copy after login
Copy after login
Copy after login

Summary

Find the difference between two arrays in PHP Values ​​can be retrieved in a variety of ways, including using the array_diff() function, loops and the in_array() function, the array_diff_key() function, and the array_udiff() function. Select the appropriate method to implement the difference calculation based on specific needs and performance requirements .

The above is the detailed content of How to find the difference between two arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!