Php's special intersection of these two arrays seems simple, but it is not actually a solution.

Source: Internet
Author: User
Php calculates the special intersection of these two arrays. it seems simple. In fact, $ x = array (& nbsp; array (& quot; a & quot;, 2), & nbsp; array (& quot; B & quot;, 4), & nbsp; array (& quot; e & quot;, 3); $ y = array (& quot; B & quot;, & quot; php calculates the special intersection of the two arrays, which seems simple but not actually
$ X = array (
Array ("a", 2 ),
Array ("B", 4 ),
Array ("e", 3)
);
$ Y = array ("B", "f ");

The following result is required:
Array (
Array ("B", 4 ),
Array ("f", 0)
)

The next thought is to generate a new array based on $ y. because $ y has two elements, the new array also has two elements. because $ x has B, therefore, B takes the value of $ x, that is, 4. because $ x does not contain f, f takes 0.
How does PHP get such a result? Grateful!

------ Solution --------------------
PHP code
Foreach ($ x as $ v) $ tmp [$ v [0] = $ v [1]; foreach ($ y as $ B) $ ar [] = array ($ B, $ tmp [$ B]? $ Tmp [$ B]: 0); print_r ($ ar );

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.