Find an array algorithm

Source: Internet
Author: User
The result of "Array 3" is obtained according to "Array 1" and "Array 2", and it is better to be efficient.
Array 2 is a set of properties; Array 1 is a product that may fill only one or a few properties

Array 1Array ([100008]/= Array ([5] = = Array ([name] = Price Lattice [0] = = not limited to) [6] = = Array ([name] = > Discount [0] = not limited to)] [100009] = = Array ([5] = Ar Ray ([name] = Price [8] = 0-29) [6] = = Array ([name] = discount [30 percent-50 percent] + +) [7]        = = Array ([name] + press [+] = = Peking University Press) )//array 2Array ([0] = = Array ([attr_id] = 5 [Attr_name] = Price [at Tr_value] = 0-29,30-49,50-99,100 above) [1] = = Array ([attr_id] = 6 [attr_          Name] = Discount  [Attr_value] = 00 percent-30 percent, 30 percent-50 percent, 50 percent-70 percent, 70 percent-10 percent) [2] = = Array ([attr_id] = 7 [at Tr_name] + press [Attr_value]//Get Results: Array 3Array ([100008] + = Array ([Price ] = [Discount] = not limited [publisher] = [100009] = = Array ([Price] =&gt ; 0-29 [discount] + 30 percent-50 percent [Publisher] = Peking University Press)



Reply to discussion (solution)

$a = Array (100008 = = Array (5 = = = ' Price ', 0 = ' unlimited ',), 6 = = Array (      ' Name ' = ' Discount ', 0 = ' unlimited ', ', '), 100009 = = Array (5 = = Array (' name ' = ' price '), 8 = ' 0-29 ',), 6 = = Array (' name ' = ' discount ', ' 30 percent-50 percent ', '), 7 = = Array (' Nam E ' = = ' Press ', ' Beijing University Press ',),); $b = Array (0 = = Array (' attr_id ' = ' 5 ', ' attr_name ' = > ' Price ', ' attr_value ' = ' + ' 0-29,30-49,50-99,100 ', ', ' 1 = = Array (' attr_id ' = ' 6 ', ' Attr_name ' =&G T ' Discount ', ' attr_value ' = ' 00 percent-30 percent, 30 percent-50 percent, 50 percent-70 percent, 70 percent-10 percent ', ', ' 2 = ' array (' attr_id ' = ' 7 ', ' attr_name ' = ' + ' Press  ', ' attr_value ' = ', '); foreach ($a as $key = = $item) {$t = array ();    foreach ($b as $v) {$k = $v [' attr_id ']; $t [$v [' attr_name ']] = Isset ($item [$k])?  End ($item [$k]): $v [' Attr_value ']; } $c [$key] = $t;} Print_r ($c);
Array ([    100008]/= Array        ([price] = not limited to [            discount] =            [Publisher] = [    100009] = = Array        (            [Price] = 0-29            [discount] = 30 percent-50 percent            [publishing house] = = Peking University Press        ))

Moderator, cow ....

  • 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.