3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
<?php echo "<pre>";$test = array("test_1","test_2","test_3");// Get Combination$return = uniqueCombination($test);//Sortsort($return);//Pretty Printprint_r(array_map(function($v){ return implode(",", $v); }, $return));function uniqueCombination($in, $minLength = 1, $max = 2000) { $count = count($in); $members = pow(2, $count); $return = array(); for($i = 0; $i < $members; $i ++) { $b = sprintf("%0" . $count . "b", $i); $out = array(); for($j = 0; $j < $count; $j ++) { $b{$j} == '1' and $out[] = $in[$j]; } count($out) >= $minLength && count($out) <= $max and $return[] = $out; } return $return;}?>
輸出量
Array
(
[0] => test_1
[1] => test_2
[2] => test_3
[3] => test_1,test_2
[4] => test_1,test_3
[5] => test_2,test_3
[6] => test_1,test_2,test_3
)

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
Math_Combinatorics
返回所有組合和排列的包,不重復(fù)、給定集和子集的大小。關(guān)聯(lián)數(shù)組被保留。
require_once 'Math/Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$input = array(1, 2, 3, 4, 5, 6, 7);
$output = $combinatorics->combinations($input, 5); // 5 is the subset size
// 1,2,3,4,5
// 1,2,3,4,6
// 1,2,3,4,7
// 1,2,3,5,6
// 1,2,3,5,7
// 1,2,3,6,7
// 1,2,4,5,6
// 1,2,4,5,7
// 1,2,4,6,7
// 1,2,5,6,7
// 1,3,4,5,6
// 1,3,4,5,7
// 1,3,4,6,7
// 1,3,5,6,7
// 1,4,5,6,7
// 2,3,4,5,6
// 2,3,4,5,7
// 2,3,4,6,7
// 2,3,5,6,7
// 2,4,5,6,7
// 3,4,5,6,7
- 3 回答
- 0 關(guān)注
- 310 瀏覽
添加回答
舉報(bào)