This post was last edited by Sibang on 2013-10-16 22:08:17
', Var_dump ($x 3), '
The expected/**: array {' ADG ', ' adh ', ' Adi ', ' AEG ', ' Aeh ', ' AEI ', ' AFG ', ' afh ', ' AFI ', ' BDG ', ' bdh ', ' BDI ', ' beg ', ' beh ', ' bei ', ' BFG ', ' bfh ', ' BFI ', ' CDG ', ' CDH ', ' CDI ', ' CEG ', ' CEH ', ' CEI ', ' cfg ', ' cfh ', ' CFI ',}/**/$x 2=iarray ($arr, 2); Echo '
', Var_dump ($x 2), '
';/** expect? The results are: array {' ad ', ' AE ', ' af ', ' AG ', ' ah ', ' ai ', ' BD ', ' be ', ' bf ', ' bg ', ' bh ', ' bi ', ' CD ', ' CE ', ' cf ', ' CG ', ' ch ', ' ci ', ' DG ', ' dh ', ' di ', ' eg ', ' eh ', ' ei ', ' fg ', ' FH ', ' fi ',}/**/
Reply to discussion (solution)
A lot of flowers.
$arr =array ( array (' A ', ' B ', ' C '), array (' d ', ' e ', ' f '), array (' g ', ' h ', ' I '), //second ' array '?? It's not known, it's a convenient example, so it's only 3?); Print_r (IArray ($arr, 3));p Rint_r (IArray ($arr, 2)); function IArray ($ar, $n) { if ($n = = 2) {//3 takes 2 combination $ar = Array ($ar [ 0], $ar [1]), array ($ar [0], $ar [2 ]), Array ($ar [1], $ar [2]), ); } else $ar = Array ($ar); $res = Array (); foreach ($ar as $d) { $r = Array_pop ($d); while ($d) { $t = array (); foreach (Array_pop ($d) as $x) foreach ($r as $y) $t [] = $x. $y; $r = $t; } $res = Array_merge ($res, $r); } return $res;}
Array
(
[0] = ADG
[1] = ADH
[2] = = Adi
[3] = + AEG
[4] = Aeh
[5] = AEI
[6] = AFG
[7] = AFH
[8] = AFI
[9] = BDG
[Ten] = BDH
[One] = BDI
[[] = Beg
[+] = Beh
[+] = bei
[[] = BFG
[+] = BFH
[+] = BFI
[+] = CDG
[+] = CDH
[+] = CDI
[+] = CEG
[] = CEH
[+] = CEI
[+] = cfg
[+] = CFH
[+] = CFI
)
Array
(
[0] = = AD
[1] = = AE
[2] + = AF
[3] = BD
[4] = be
[5] = = BF
[6] = = CD
[7] = CE
[8] = CF
[9] = = AG
[Ten] = Ah
[One] = AI
[+] = BG
[+] = BH
[+] = Bi
[+] = CG
[+] = ch
[+] = CI
[+] = DG
[+] = DH
[+] = di
[+] = eg
[+] = eh
[+] = ei
[+] = FG
[+] = fh
[+] = fi
)
$arr =array ( array (' A ', ' B ', ' C '), array (' d ', ' e ', ' f '), array (' g ', ' h ', ' I '), //second ' array '?? It's not known, it's a convenient example, so it's only 3?); Print_r (IArray ($arr, 3));p Rint_r (IArray ($arr, 2)); function IArray ($ar, $n) { if ($n = = 2) {//3 takes 2 combination $ar = Array ($ar [ 0], $ar [1]), array ($ar [0], $ar [2 ]), Array ($ar [1], $ar [2]), ); } else $ar = Array ($ar); $res = Array (); foreach ($ar as $d) { $r = Array_pop ($d); while ($d) { $t = array (); foreach (Array_pop ($d) as $x) foreach ($r as $y) $t [] = $x. $y; $r = $t; } $res = Array_merge ($res, $r); } return $res;}
Array
(
[0] = ADG
[1] = ADH
[2] = = Adi
[3] = + AEG
[4] = Aeh
[5] = AEI
[6] = AFG
[7] = AFH
[8] = AFI
[9] = BDG
[Ten] = BDH
[One] = BDI
[[] = Beg
[+] = Beh
[+] = bei
[[] = BFG
[+] = BFH
[+] = BFI
[+] = CDG
[+] = CDH
[+] = CDI
[+] = CEG
[] = CEH
[+] = CEI
[+] = cfg
[+] = CFH
[+] = CFI
)
Array
(
[0] = = AD
[1] = = AE
[2] + = AF
[3] = BD
[4] = be
[5] = = BF
[6] = = CD
[7] = CE
[8] = CF
[9] = = AG
[Ten] = Ah
[One] = AI
[+] = BG
[+] = BH
[+] = Bi
[+] = CG
[+] = ch
[+] = CI
[+] = DG
[+] = DH
[+] = di
[+] = eg
[+] = eh
[+] = ei
[+] = FG
[+] = fh
[+] = fi
)
?? May be:
$arr =array (
Array (' A ', ' B ', ' C ', ' J '),
Array (' d ', ' e ', ' f '),
Array (' g ', ' h ', ' I '),
Array (' K ', ' l ', ' m '),
);
It may also be:
$arr =array (
Array (' A ', ' B ', ' C ', ' J '),
Array (' d ', ' e ', ' f '),
Array (' g ', ' h ', ' I '),
Array (' K ', ' l ', ' m '),
Array (' 2 ', ' 1 ', ' 3 ', ' SD ', ' EE '),
);
IArray ($ar, $n)
$n value?? Yes: 2~6
I've already indicated in the code that I need to find the combination of M-fetch N first.
and the combination of functions that you already have in your code base.
That's what I'm using.
Combination combination function combination ($arr, $num =0) { $len = count ($arr); if ($num = = 0) $num = $len; $res = Array (); For ($i =1, $n =pow (2, $len), $i < $n, + + $i) { $tmp = Str_pad (Base_convert ($i, 2), $len, ' 0 ', str_pad_left); $t = Array (); for ($j =0; $j < $len; + + $j) { if ($tmp {$j} = = ' 1 ') { $t [] = $arr [$j]; } } if (count ($t) = = $num) $res [] = $t; } return $res;}
So the IArray function can be written
function IArray ($ar, $n) { $res = array (); foreach (Combination ($ar, $n) as $d) { $r = Array_pop ($d); while ($d) { $t = array (); foreach (Array_pop ($d) as $x) foreach ($r as $y) $t [] = $x. $y; $r = $t; } $res = Array_merge ($res, $r); } return $res;}