这是一个经常重复的问题的一个特殊变体,但尽我所能,我无法在 Stack Overflow 上的任何地方找到这种确切的情况。
长话短说,我想采用这样的数组:
$days[0] = 'Monday';
$days[1] = 'Tuesday';
$days[2] = 'Thursday';
($days 可以包含一周中五个工作日的任意数量和组合。)
然后,给定 $numberOfDays
的特定值(当然,它必须至少为 1 且不超过 $days
的计数),我想要一个包含 $days 与 $numberOfDays 天数的所有可能组合的数组。
例如:
$days[0] = 'Monday';
$days[1] = 'Tuesday';
$days[2] = 'Thursday';
$numberOfDays = 2;
$dayCombinations = getDayCombinations($days, $numberOfDays);
输出:
$dayCombinations[0] = array("Monday", "Tuesday");
$dayCombinations[1] = array("Monday", "Thursday");
$dayCombinations[2] = array("Tuesday", "Thursday");
请注意,这些是组合,而不是排列,因此顺序无关紧要。
如果有帮助,我找到了这个函数 here : 它运行良好,但包括重复,并且基于字符串而不是数组(最后一部分是可行的,没什么大不了的,但重复部分对我来说真的很糟糕)。
function sampling($chars, $size, $combinations = array()) {
# if it's the first iteration, the first set
# of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $chars;
}
# we're done if we're at size 1
if ($size == 1) {
return $combinations;
}
# initialise array to put new values in
$new_combinations = array();
# loop through existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($chars as $char) {
$new_combinations[] = $combination . $char;
}
}
# call same function again for the next iteration
return sampling($chars, $size - 1, $new_combinations);
}
更新:我试图用条件来包装分配 $new_combinations
的行以提供帮助;这根本没有效果,尽管我不确定为什么。所有组合仍然存在,即使是重复的组合。
function sampling($chars, $size, $combinations = array()) {
# if it's the first iteration, the first set
# of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $chars;
}
# we're done if we're at size 1
if ($size == 1) {
return $combinations;
}
# initialise array to put new values in
$new_combinations = array();
# loop through existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($chars as $char) {
if (strpos($combination, $char) === FALSE) {
echo "Char $char not found in Combination $combination<br>";
$new_combinations[] = $combination . $char;
}
}
}
# call same function again for the next iteration
return sampling($chars, $size - 1, $new_combinations);
}
那里的输出返回奇怪的东西,比如:
Char 2 not found in Combination 2
Char 3 not found in Combination 23
等等。
感谢您的帮助!
亚历克斯
最佳答案
基本相同的结构,只是在添加之前检查组合中是否已经有日期。
function sampling($days, $size, $combinations = array()) {
# if it's the first iteration, the first set
# of combinations is the same as the set of days
if (empty($combinations)) {
$combinations = array_map(function($day) { return array($day); }, $days);
}
# we're done if we're at size 1
if ($size == 1) {
return $combinations;
}
# initialise array to put new values in
$new_combinations = array();
# loop through existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($days as $day) {
if (!in_array($day, $combination)) {
$new_combination = $combination;
$new_combination[] = $day;
$new_combinations[] = $new_combination;
}
}
}
# call same function again for the next iteration
return sampling($days, $size - 1, $new_combinations);
}
关于php - PHP中特定计数的非重复组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41411473/