In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how php obtains the intersection of multiple arrays. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Array_intersect function-compare the key values of an array
The built-in array_intersect function in PHP is used to compare the key values of the array when comparing the array, and then return the key values of both arrays with the elements of the array, that is, the intersection of the two arrays.
The basic syntax format of the array_intersect function is as follows:
Array_intersect (array1,array2,array3...)
It is important to note that the parameter array123 represents the array used for comparison, array 1 is used to compare with other arrays, and arrays 2 and 3 are used to compare with array 1. When a function compares two or more arrays, the result returned is an intersecting array.
In this returned array, only array 1 intersects with other arrays, and the key name is also of array 1, that is, the returned array is part of array 1, and the other arrays are compared with array 1 only by key values. does not appear in the returned array. Only key values are compared.
Next, let's take a look at the application of the array_intersect function through an example, as follows:
Output result:
In the above example, the two arrays are compared. what you can see is that there are two key values in array 1 that are the same as array 2, and the key name in the output is still the key name of array 1.
In the above example, we compare two arrays. Let's take an example and compare three arrays. The example is as follows:
Output result:
Next, let's take a look at the array_intersect_key function in PHP. Instead of comparing key values, it compares key names.
Array_intersect_key function-compare the key name of the array
The array_intersect_key function is very similar to the array_intersect function, but the comparison is different. The array_intersect_key function obtains the intersection of the same key name by comparing the key name.
The basic syntax format of the array_intersect_key function is as follows:
Array_intersect_key (array1,array2,array3...)
It should be noted that the parameter array123 represents the array used for comparison, array 1 is used to compare with other arrays, and arrays 2 and 3 are used to compare with array 1. When a function compares two or more arrays, the result returned is an intersecting array.
The returned function is similar to array_intersect, and the returned result is the corresponding key value in the array of intersection key names array 1. Only compare key names.
Let's take a look at the use of the array_intersect_key function through an example, as follows:
First of all, let's take a look at an array that does not define a key name, but only through an index.
Output result:
When there is a key name that defines the key value, it is the same, but it is OK to compare the key name. The example is as follows:
Output result:
The above example is the operation of comparing key names to obtain intersection through the array_intersect_key function.
Array_intersect_assoc function-compare the key name and key value of an array
The above two functions one compares the key value and the other compares the key name. Now PHP can compare the key value and the key name at the same time through the array_intersect_assoc function, that is to say, the output result must be the key value and the key name are equal.
The basic syntax format of the array_intersect_assoc function is as follows:
Array_intersect_assoc (array1,array2,array3...)
It should be noted that the parameter array123 represents the array used for comparison, array 1 is used to compare with other arrays, and arrays 2 and 3 are used to compare with array 1. When a function compares two or more arrays, the result returned is an intersecting array.
Since the returned intersection array has the same key name and key value, the returned result does not need to be debated.
Next, let's take a look at the application of the array_intersect_assoc function through an example, as follows:
First of all, let's take a look at an array without a defined key name, but only through an index.
Output result:
Let's take a look at the array with custom key names.
Output result:
This is the end of this article on "how to get the intersection of multiple arrays in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.