Does php array have intersection?

PHPz
Release: 2023-05-19 15:29:38
Original
623 people have browsed it

PHP Whether arrays have intersection

In PHP, sometimes you need to perform some operations on arrays. One of the common operations is to determine whether two arrays have intersection. If two arrays intersect, then they have at least one element in common. This operation is very common in actual development. For example, in the user registration system, it is necessary to determine whether the email address entered by the user has been registered by other users; or in the product inventory management system, it is necessary to determine whether a certain product has been purchased by other orders.

In PHP, there are many ways to determine whether two arrays intersect. This article will introduce several of them.

  1. array_intersect function

PHP provides the array_intersect function, which can be used to compare the intersection of two or more arrays. Its usage is as follows:

$array1 = array('a', 'b', 'c', 'd'); $array2 = array('c', 'd', 'e', 'f'); $result = array_intersect($array1, $array2); print_r($result);
Copy after login

The output result is as follows:

Array ( [2] => c [3] => d )
Copy after login

This function returns an array, and the elements in the array are the intersection of the two input arrays. In this example, the intersection of the input arrays $array1 and $array2 is the array array('c', 'd') .

  1. array_intersect_key function

In addition to the array_intersect function, PHP also provides the array_intersect_key function. Unlike the array_intersect function, the array_intersect_key function compares the key values of the array rather than the values themselves. Its usage is as follows:

$array1 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); $array2 = array('c' => 3, 'd' => 4, 'e' => 5, 'f' => 6); $result = array_intersect_key($array1, $array2); print_r($result);
Copy after login

The output is as follows:

Array ( [c] => 3 [d] => 4 )
Copy after login
Copy after login

This function returns an array whose elements are the intersection of the keys of the two input arrays. In this example, the intersection of the keys of the input arrays $array1 and $array2 is the array array('c' => 3, 'd' => 4) .

  1. Two foreach loops

Another method is to use two foreach loops to compare the values of the two arrays. Its usage is as follows:

$array1 = array('a', 'b', 'c', 'd'); $array2 = array('c', 'd', 'e', 'f'); $result = false; foreach ($array1 as $value1) { foreach ($array2 as $value2) { if ($value1 == $value2) { $result = true; break 2; } } } if ($result) { echo '两个数组有交集'; } else { echo '两个数组没有交集'; }
Copy after login

In the above code, we first set the value of $result to false, indicating that the two arrays have no intersection. Then use two foreach loops to traverse the two arrays respectively. If the same elements are found, set the value of $result to true and use the break 2 statement to jump out of the two loops. Finally, the result is output based on the value of $result.

  1. array_intersect_ukey function

In addition to the functions introduced above, PHP also provides the array_intersect_ukey function. Similar to the array_intersect_key function, the array_intersect_ukey function compares the key values of the array, but it requires an additional callback function to be passed to compare the two keys. Its usage is as follows:

$array1 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); $array2 = array('C' => 3, 'D' => 4, 'e' => 5, 'f' => 6); $result = array_intersect_ukey($array1, $array2, 'strcasecmp'); print_r($result);
Copy after login

The output is as follows:

Array ( [c] => 3 [d] => 4 )
Copy after login
Copy after login

This function returns an array whose elements are the intersection of the keys of the two input arrays. In this example, the intersection of the keys of the input arrays $array1 and $array2 is the array array('c' => 3, 'd' => 4) . Note that we passed a callback function 'strcasecmp' which is used to compare keys case-insensitively.

Summary

This article introduces various methods in PHP to determine whether two arrays intersect, including array_intersect, array_intersect_key, two foreach loops and the array_intersect_ukey function. In actual development, we can choose the appropriate method according to the specific situation in order to achieve the best performance and efficiency.

The above is the detailed content of Does php array have intersection?. For more information, please follow other related articles on the PHP Chinese website!

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 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!