How to Use the keys of two arrays to calculate the intersection

Source: Internet
Author: User
{Code ...} there are two arrays above, now, you need to know which of the preceding statements are repeated. For example, if the key in the array permission is MainIndextestb, and the key in the menu is MainIndextest, the second statement is repeated. For example, in the array permission key is Ma...
A:array(4) {  ["Api/Index/index"] => string(3) "226"  ["Main/Index/index"] => string(3) "227"  ["Main/Index/main"] => string(3) "228"  ["/Main/Index/test"] => string(3) "229"}B:array(22) {  ["/Main/Index/test"] => int(3)  ["/Main/Index/liebiao/text/%E5%88%97%E8%A1%A8%E6%B5%8B%E8%AF%95"] => string(7) "0-sec-0"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E5%88%86%E6%9E%902"] => string(7) "0-sec-1"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E5%88%86%E6%9E%903"] => string(7) "0-sec-2"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E6%95%B0%E6%8D%AE1"] => string(7) "1-sec-0"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E6%95%B0%E6%8D%AE2"] => string(7) "1-sec-1"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E6%95%B0%E6%8D%AE3"] => string(7) "1-sec-2"  ["/Main/Index/test/text/%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%861"] => string(7) "2-sec-0"  ["/Main/Index/test/text/%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%862"] => string(7) "2-sec-1"  ["/Main/Index/test/text/%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%863"] => string(7) "2-sec-2"  ["/Main/Index/test/text/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E7%BD%AE1"] => string(7) "3-sec-0"  ["/Main/Tree/index/text/%E6%A0%91%E7%8A%B6%E7%AE%A1%E7%90%86"] => string(7) "3-sec-1"  ["/Main/Test/index/text/%E5%88%97%E8%A1%A8%E6%B5%8B%E8%AF%95"] => string(7) "3-sec-2"  ["/Ucenter/User/index/text/%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86"] => string(7) "3-sec-3"  ["/System/Config/index/text/%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86"] => string(7) "3-sec-4"  ["/System/Index/index"] => int(4)  ["/System/Subject/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-0"  ["/System/Industry/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-1"  ["/System/Area/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-2"  ["/System/FlatRate/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-3"  ["/System/Settlement/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-4"  ["/System/Split/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-5"}

With the above two arrays, you need to know which duplicate
How is it repeated?

In the first case, for example, if the key in array a is/Main/Index/test B, the key in the array menu is/Main/Index/test, which is repeated.

In the second case, for example, if the key in the array permission is Main/Index/test B, the key in the array menu is/Main/Index/test/text/fdsfsdfljl, it is also counted as duplicated data.

In both cases, the key of array B is put into array c as the intersection.
How can I use less time to calculate the intersection of the two arrays.
All I can think of is that foreach matches a single record in a loop, but when there are hundreds of rules in a and hundreds of menu items in B, there will be a more obvious choppy phenomenon. I don't know who has a better way to reduce the number of cycles and save the running time.

Reply content:
A:array(4) {  ["Api/Index/index"] => string(3) "226"  ["Main/Index/index"] => string(3) "227"  ["Main/Index/main"] => string(3) "228"  ["/Main/Index/test"] => string(3) "229"}B:array(22) {  ["/Main/Index/test"] => int(3)  ["/Main/Index/liebiao/text/%E5%88%97%E8%A1%A8%E6%B5%8B%E8%AF%95"] => string(7) "0-sec-0"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E5%88%86%E6%9E%902"] => string(7) "0-sec-1"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E5%88%86%E6%9E%903"] => string(7) "0-sec-2"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E6%95%B0%E6%8D%AE1"] => string(7) "1-sec-0"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E6%95%B0%E6%8D%AE2"] => string(7) "1-sec-1"  ["/Main/Index/test/text/%E7%A8%8E%E6%94%B6%E6%95%B0%E6%8D%AE3"] => string(7) "1-sec-2"  ["/Main/Index/test/text/%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%861"] => string(7) "2-sec-0"  ["/Main/Index/test/text/%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%862"] => string(7) "2-sec-1"  ["/Main/Index/test/text/%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%863"] => string(7) "2-sec-2"  ["/Main/Index/test/text/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E7%BD%AE1"] => string(7) "3-sec-0"  ["/Main/Tree/index/text/%E6%A0%91%E7%8A%B6%E7%AE%A1%E7%90%86"] => string(7) "3-sec-1"  ["/Main/Test/index/text/%E5%88%97%E8%A1%A8%E6%B5%8B%E8%AF%95"] => string(7) "3-sec-2"  ["/Ucenter/User/index/text/%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86"] => string(7) "3-sec-3"  ["/System/Config/index/text/%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86"] => string(7) "3-sec-4"  ["/System/Index/index"] => int(4)  ["/System/Subject/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-0"  ["/System/Industry/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-1"  ["/System/Area/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-2"  ["/System/FlatRate/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-3"  ["/System/Settlement/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-4"  ["/System/Split/index/text/%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"] => string(7) "4-sec-5"}

With the above two arrays, you need to know which duplicate
How is it repeated?

In the first case, for example, if the key in array a is/Main/Index/test B, the key in the array menu is/Main/Index/test, which is repeated.

In the second case, for example, if the key in the array permission is Main/Index/test B, the key in the array menu is/Main/Index/test/text/fdsfsdfljl, it is also counted as duplicated data.

In both cases, the key of array B is put into array c as the intersection.
How can I use less time to calculate the intersection of the two arrays.
All I can think of is that foreach matches a single record in a loop, but when there are hundreds of rules in a and hundreds of menu items in B, there will be a more obvious choppy phenomenon. I don't know who has a better way to reduce the number of cycles and save the running time.

There are official functions that can do this directly... it looks like this with handwriting:

array_intersect_uassoc($a, $b, function($ka, $kb) {    if( preg_match("%$ka%i", $kb) ) return 0;    return $ka - $kb;});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.