php 数组 任意 排列 组合
和部门同时讨论了一上午了,没有讨论出结果来,基本需求如下:用现成的函数实现,未作优化
$a = array('a', 'b', 'c', 'd');$res = array();foreach(arrangement($a) as $v) { $res = array_merge($res, foo(explode(' ', trim($v))));}print_r($res);//排列 arrangementfunction arrangement($arr = array(), $res = '') { if(! is_array($arr) ) $arr = str_split($arr); 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 [10] => ab+dc [11] => a+b+dc [12] => abd+c [13] => a+bd+c [14] => ab+d+c [15] => a+b+d+c [16] => acdb [17] => a+cdb [18] => ac+db [19] => a+c+db [20] => acd+b [21] => a+cd+b [22] => ac+d+b [23] => a+c+d+b [24] => acbd [25] => a+cbd [26] => ac+bd [27] => a+c+bd [28] => acb+d [29] => a+cb+d [30] => ac+b+d [31] => a+c+b+d [32] => adbc [33] => a+dbc [34] => ad+bc [35] => a+d+bc [36] => adb+c [37] => a+db+c [38] => ad+b+c [39] => a+d+b+c [40] => adcb [41] => a+dcb [42] => ad+cb [43] => a+d+cb [44] => adc+b [45] => a+dc+b [46] => ad+c+b [47] => a+d+c+b [48] => bcda [49] => b+cda [50] => bc+da [51] => b+c+da [52] => bcd+a [53] => b+cd+a [54] => bc+d+a [55] => b+c+d+a [56] => bcad [57] => b+cad [58] => bc+ad [59] => b+c+ad [60] => bca+d [61] => b+ca+d [62] => bc+a+d [63] => b+c+a+d [64] => bdac [65] => b+dac [66] => bd+ac [67] => b+d+ac [68] => bda+c [69] => b+da+c [70] => bd+a+c [71] => b+d+a+c [72] => bdca [73] => b+dca [74] => bd+ca [75] => b+d+ca [76] => bdc+a [77] => b+dc+a [78] => bd+c+a [79] => b+d+c+a [80] => bacd [81] => b+acd [82] => ba+cd [83] => b+a+cd [84] => bac+d [85] => b+ac+d [86] => ba+c+d [87] => b+a+c+d [88] => badc [89] => b+adc [90] => ba+dc [91] => b+a+dc [92] => bad+c [93] => b+ad+c [94] => ba+d+c [95] => b+a+d+c [96] => cdab [97] => c+dab [98] => cd+ab [99] => c+d+ab [100] => cda+b [101] => c+da+b [102] => cd+a+b [103] => c+d+a+b [104] => cdba [105] => c+dba [106] => cd+ba [107] => c+d+ba [108] => cdb+a [109] => c+db+a [110] => cd+b+a [111] => c+d+b+a [112] => cabd [113] => c+abd [114] => ca+bd [115] => c+a+bd [116] => cab+d [117] => c+ab+d [118] => ca+b+d [119] => c+a+b+d [120] => cadb [121] => c+adb [122] => ca+db [123] => c+a+db [124] => cad+b [125] => c+ad+b [126] => ca+d+b [127] => c+a+d+b [128] => cbda [129] => c+bda [130] => cb+da [131] => c+b+da [132] => 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 [140] => 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 [150] => da+b+c [151] => d+a+b+c [152] => 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 [160] => 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 [170] => db+ac [171] => d+b+ac [172] => dba+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 [180] => 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+cb+a [190] => dc+b+a [191] => d+c+b+a)
多谢版主,函数我有话下,去重就OK啦。
去重后再次贴出,多谢先,稍后确认。
有重复的吗?显然没有
print_r(array_unique($res));
和
print_r($res);
的结果一样!
var_dump(count($res) == count(array_unique($res)));
bool(true)
抱歉版主,我没有解释清楚,A+B+C+D 和 B+A+C+D, D+C+A+B等算一种。