PHP drop box, that is: multiple arrays are arbitrarily arranged into a large array, and must contain all the original array

Source: Internet
Author: User
PHP array arbitrary permutation combination

And the department discussed it all morning, without discussing the results, the basic requirements are as follows:
Assume:
$a = Array (' a ');
$b = Array (' B ');
$c = Array (' C ');
$d = Array (' d ');

Get the following result from a function:
Array
A+b+c+d,
Ab+c+d,
Ac+b+d,
Ad+b+c,
Bc+ad,
Bc+a+d,
Bd+ac,
Bd+a+c
Cd+ab,
Cd+a+b,
Abc+a,
Acd+b,
Abd+c,
Bcd+a,
...
)

That is, to get the combination of all arrays, any combination of array ordering is not necessarily in order, but requires that the combination must appear all elements.
Is there an expert to give a good algorithm or idea?


Reply to discussion (solution)

Implemented with ready-made functions, not optimized

 $a = Array (' A ', ' B ', ' C ', ' d '); $res = Array (); foreach (arrangement ($a) as $v) {$res = arr Ay_merge ($res, foo (Explode (", trim ($v))));} Print_r ($res);//Arrange arrangementfunction arrangement ($arr = Array (), $res = ") {if (! Is_array ($arr)) $arr = Str_split ($a  RR);  if (empty ($arr)) $array [] = $res;    else foreach ($arr as $k = + $v) {unset ($arr [$k]); foreach (Arrangement ($arr, $res.    "$v") as $t) $array [] = $t;  $arr [$k] = $v; } return $array;}  function foo ($ar) {$res = array ();    if (count ($ar) > 2) {$t = Array_shift ($ar);       foreach (foo ($ar) as $v) {$res [] = "$t $v";    $res [] = "$t + $v";     }}else {$res [] = "$ar [0] $ar [1]";  $res [] = "$ar [0]+ $ar [1]"; } return $res;} 
Array ([0] = ABCD [1] = A+BCD [2] = AB+CD [3] = A+B+CD [4] = = Abc+d [5] = = A+bc+d    [6] = ab+c+d [7] = a+b+c+d [8] = ABDC [9] = A+BDC [Ten] = AB+DC [one] = A+B+DC    [[] = abd+c [] = a+bd+c [+] = ab+d+c [] = a+b+d+c [+] = acdb [+] = a+cdb     [+] = ac+db [] = a+c+db [+] = acd+b [+] = a+cd+b [] = ac+d+b [] = A+c+d+b    [[] = ACBD [] = A+CBD [] = AC+BD [+] = A+C+BD [+] = acb+d [+] = A+cb+d     [+] = ac+b+d [+] = a+c+b+d [+] = ADBC [+] = A+DBC [+] = AD+BC [+] = A+D+BC [[+] = adb+c [PNS] = a+db+c [] = ad+b+c [] = a+d+b+c [+] = ADCB [+] = A+DC b [[+] = AD+CB [] = A+D+CB [] + adc+b [[] = A+dc+b [] [] = ad+c+b [+] = A+d +c+b [[] = BCDA   [+] = B+CDA [[[]] = Bc+da [Wuyi] + b+c+da [] = bcd+a [+] = b+cd+a [si] = bc+d+a     [[+] = b+c+d+a [[+]] = BCAD [[+]] = B+cad [+] = Bc+ad [+] = B+c+ad [+] = Bca+d [B+ca+d] = = [+] = bc+a+d [+] = b+c+a+d [+] = BDAC [+] = B+DAC [$] = Bd+a c [[+] = B+d+ac [] = bda+c [] = b+da+c [] = Bd+a+c [[] = [] = B+d+a+c [] + b DCA [B+DCA] = [all] = BD+CA [[]] = B+D+CA [[] [] = bdc+a [All] = b+dc+a [+] = BD +c+a [[+] = b+d+c+a [] = BACD [Bayi] = B+ACD [[+] = BA+CD [the] [] = B+A+CD [+] = b Ac+d [[] = b+ac+d [[+] = ba+c+d [] [[]] = b+a+c+d [A] = [[]] [] = BADC [[]] = = BA+DC [[] = [] = B+A+DC [[]] = bad+c [] = b+ad+c [94] = Ba+d+c [up] = B+a+d+c [] =&G T Cdab [+] = c+DAB [98] = Cd+ab [in] = C+d+ab [+] = cda+b [101] = c+da+b [102] = cd+a+b [103] =&G T c+d+a+b [104] = CDBA [[+] = C+DBA [106] = CD+BA [107] = C+D+BA [108] = Cdb+a [109]    = C+db+a [[+] = cd+b+a [111] = C+d+b+a [in] + cabd [113] = c+abd [all] = CA+BD  [C+A+BD] = [$] = cab+d [117] = c+ab+d [118] = ca+b+d [119] = c+a+b+d [+] = CADB [121] = c+adb [122] = CA+DB [123] = c+a+db [124] = Cad+b [[]] = c+ad+b [126]    = ca+d+b [127] = c+a+d+b [+] = CBDA [129] = C+BDA [[] [] = Cb+da [131] = C+b+da [PPI] = cbd+a [133] = C+bd+a [134] = Cb+d+a [135] = c+b+d+a [136] = Cbad [137] + C +bad [138] = Cb+ad [139] = C+b+ad [[+] = cba+d [141] = c+ba+d [142] = cb+a+d [143] = C+b+a+d [144] = DABC [145] = D+ABC [146] = DA+BC [147] = D+A+BC [148] = dab+c [149] + d+ab+c [Da+b+c] = [151] = d+a+b+c [] = DACB [153] = D+ACB [154] = DA+CB [155] = d+ A+CB [156] = dac+b [157] = d+ac+b [158] = da+c+b [159] = D+a+c+b [[]] = DBCA [161]    = D+BCA [162] = DB+CA [163] = D+B+CA [164] = dbc+a [165] = D+bc+a [166] = Db+c+a [167] = d+b+c+a [168] = DBAC [169] = D+BAC [[+] = db+ac [171] = D+B+AC [172] = db a+c [173] = d+ba+c [174] = db+a+c [175] = d+b+a+c [176] = Dcab [177] = = D+cab [178] =    > dc+ab [179] = D+c+ab [[+] = dca+b [181] = d+ca+b [182] = dc+a+b [183] = D+c+a+b [184] = DCBA [185] = D+CBA [186] = Dc+ba [187] = D+C+BA [188] = dcb+a [189] = D+c B+a [] = dc+b+A [191] = d+c+b+a) 

Thank you moderator, function I have words, go to heavy OK.
Go to the heavy after again post, more Mr. Chia, later confirmed.

Is there a repetition? Apparently not.
Print_r (Array_unique ($res));
And
Print_r ($res);
The same result!

Var_dump (count ($res) = = Count (Array_unique ($res)));
BOOL (TRUE)

Sorry moderator, I did not explain clearly, a+b+c+d and B+a+c+d, D+c+a+b and so on.

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