Home >Backend Development >PHP Problem >PHP array learning: How to compare two arrays and find the difference set
In the previous article, we introduced the method of comparing arrays to obtain the same elements (intersection). If you are interested, you can click to read → "PHP Array Learning: How to Compare Two Arrays to Find the Intersection". Since the intersection can be found, the difference set can also be found. How to obtain the difference set? This article will introduce to you several ways to compare arrays in PHP to obtain different elements (difference sets).
Same as the previous article, let’s first take a look at the function that compares two arrays and finds the difference. There are multiple functions built into PHP. This article mainly introduces three commonly used functions: array_diff(), array_diff_key(), array_diff_assoc(), they will compare arrays in terms of "key value", "key name", "key value and key name" respectively, and return a difference array. The difference elements will be from the compared array ( first parameter).
It’s hard to understand in vain. Let’s use code examples to see how array_diff(), array_diff_key() and array_diff_assoc() compare arrays and find differences.
We have the following two arrays:
$arr1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $arr2=array("a"=>"orange","b"=>"green","e"=>"red","r"=>"yellow");
1. Use the array_diff() function--compare the key values of the array
array_diff($arr1,$arr2...)
The function only compares the key values of the array and returns a difference array. The elements in the difference array exist in the compared array$arr1
in, but does not exist in other parameter arrays $arr2...
.
Let’s take a look at the above example. When comparing the $arr1
array with the $arr2
array, the key values that only exist in the $arr1 array are: “ blue
", so the output result is:
2. Use the array_diff_key() function--compare the key names of the array
array_diff_key($arr1,$arr2...)
The function only compares the key names of the arrays, and also returns a difference array. The elements in the difference array exist in the compared array# in ##$arr1, but does not exist in other parameter arrays
$arr2....
$arr1 array and the
$arr2 array are different, with the array
$arr1 The values "
"c"=>"blue"" and "
"d"=>"yellow"" will be obtained, so the output result is:
3. Use the array_diff_assoc() function - compare the key name and key value of the array
array_diff_assoc($arr1, $arr2...)The function will compare the key name and key value of the array, and also return a difference array. The difference set elements will be from the compared array like array_diff() and array_diff_key()
Obtained from $arr1.
$arr1 array and the
$arr2 array are compared. There are three different elements, and then the value will be obtained based on the array $arr1."
"a"=>"red"", "
"c"=>"blue"", "
"d"=>"yellow"", so the output result is:
PHP function array array function video explanation, come and learn!
The above is the detailed content of PHP array learning: How to compare two arrays and find the difference set. For more information, please follow other related articles on the PHP Chinese website!