In the previous article " Learn three methods to obtain the intersection of multiple arrays in five minutes (collection) ", the relevant knowledge on how to obtain the intersection of multiple arrays in PHP array operations was introduced in detail. In this article, we will take a look at how to obtain the difference set of multiple arrays in array operations. I hope everyone has to help!
In the previous article we learned that if we want to get the intersection between multiple arrays, we can use the array_intersect
function in PHP, ## The #array_intersect_key function and the
array_intersect_assoc function use different methods to obtain the intersection between arrays. Since there is a method to obtain the intersection between arrays in PHP, there is also a method to obtain the difference between PHP arrays.
Compare key values -array_diff<span style="font-size: 20px;"></span>
Function
array_diff function is used to compare arrays by comparing key values, and then returns the difference between the arrays.
array_diffThe basic syntax format of the function is as follows:
array_diff(array1,array2,array3...);
array_diff function through an example. The example is as follows:
<?php $fruit1 = array("Apple","Banana","Orange"); $fruit2 = array("Pear","Apple","Grape"); $fruit3 = array("Watermelon","Orange","Apple"); $intersection = array_diff($fruit1, $fruit2, $fruit3); print_r($intersection); ?>
array_diff function, and the result returned is the difference between array 1 and other arrays. That is, the returned result is exactly the elements that are in array 1 but not in other arrays. Of course, this is comparing the key values of elements. Let's take a look at the difference in the results returned by comparing the key names of elements.
Compare key names-array_diff_keyFunction
array_intersect_key function Compares the key names between arrays and returns the intersection between the arrays. Similarly, the
array_diff_key function can compare the key names and return the difference between the arrays.
array_diff_keyThe basic syntax format of the function is as follows:
array_diff_key(array1,array2,array3...);
array_diff_key function through an example. The example is as follows:
<?php $fruit1 = array("a"=>"Apple","b"=>"Banana","c"=>"Orange"); $fruit2 = array("a"=>"Pear","d"=>"Apple","e"=>"Grape"); $fruit3 = array("a"=>"Watermelon","f"=>"Orange","g"=>"Apple"); $intersection = array_diff_key($fruit1, $fruit2, $fruit3); print_r($intersection); ?>
array_diff_key function to compare the key names between arrays, and then return the difference set. The returned result is exactly what is in array 1 The key name is a key name that is not found in other array elements. This is done by comparing key names. Let's take a look at how to compare key values and key names at the same time.
Compare key value and key name-array_diff_assocFunction
array_intersect_assocThe function compares the key value and the key name, and then returns the result is the intersection of the array elements. Similar to the
array_diff_assoc function, it also compares the key value and the key name, but the returned result is the intersection of the array elements. difference set.
array_diff_assocThe basic syntax format of the function is as follows:
array_diff_assoc(array1,array2,array3...);
array_diff_assoc function through an example. The example is as follows:
<?php $fruit1 = array("a"=>"Apple","b"=>"Banana","c"=>"Orange"); $fruit2 = array("a"=>"Pear","d"=>"Apple","e"=>"Grape"); $fruit3 = array("a"=>"Watermelon","f"=>"Orange","g"=>"Apple"); $intersection = array_diff_assoc($fruit1, $fruit2, $fruit3); print_r($intersection); ?>
array_diff_assoc function to compare the key values and key names between arrays, and then return the difference set. The returned result is exactly the element in array 1, but Not in other arrays.
PHP Video Tutorial" to learn more about PHP knowledge.
The above is the detailed content of Learn three methods to obtain the difference sets of multiple arrays (Collection). For more information, please follow other related articles on the PHP Chinese website!