Learn three methods to obtain the difference sets of multiple arrays (Collection)

WBOY
Release: 2023-04-10 19:30:02
Original
3656 people have browsed it

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!

Learn three methods to obtain the difference sets of multiple arrays (Collection)

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.

There are many ways to obtain the difference between arrays. Similar to obtaining the intersection, we can also compare arrays by comparing key values, comparing key names, and comparing key values ​​and key names, and then obtain the difference between arrays. set. Let’s take a look at the use of these three methods separately.

Compare key values ​​-array_diff<span style="font-size: 20px;"></span>Function

in PHP The built-in

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

It should be noted that the parameter array123 represents the array used for comparison, the returned result difference array, the elements in the array included in the first array element. The returned array is the value in the first array that is not found in other arrays, and the key names in the returned array remain unchanged.


Let’s take a look at the use of the

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

Output result:


Learn three methods to obtain the difference sets of multiple arrays (Collection)

Through the above example, we use the

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

In PHP, you can use the

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

It should be noted that: the parameter array123 represents the array that needs to be compared, and the elements in the returned difference array They are all elements in array 1, but not in other array elements.


Next let’s look at the application of the

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

Output result:


Learn three methods to obtain the difference sets of multiple arrays (Collection)

Through the above example, we use the

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

In PHP, you can pass

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

It should be noted that the parameter array123 represents the array that needs to be compared. By comparing the key value and key name, The elements in the returned difference array are all elements in array 1, but are not in other array elements.


Let’s take a look at the application of the

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

Output result:


Learn three methods to obtain the difference sets of multiple arrays (Collection)

Through the above example, we use the

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.

If you are interested, you can click on "

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!

Related labels:
source:PHP,数组
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