我正在编写一个 PHP 脚本,它本质上是一个礼品袋(从帽子里抽出名字)。但是有几个条件:
- 你不能和你的配偶配对
- 你不能拥有去年的人
- 你不能拥有自己(显然)
我写了一个代码,比如:
// I'm using a multidimensional array so I can check the spouses
$array = array(
array('Husband1', 'Wife1'),
array('Husband2', 'Wife2'),
array('Husbad3', 'Wife3'),
array('Single1'),
array('Single2'),
);
// if you were to sort the above array - here is their recipients lastYear
$lastYear = array(
'Single1',
'Husbad3',
'Single2',
'Wife3',
'Husband1',
'Wife2',
'Husband2',
'Wife1',
);
// declaring an empty values
$that = array();
$n = 0;
// converts multidimensional array to 2 identical arrays
for ($row = 0; $row < count($array); $row++)
{
for ($col = 0; $col < 2; $col++)
{
if (isset($array[$row][$col]))
{
$toList[] = $array[$row][$col];
$fromList[] = $array[$row][$col];
}
}
}
echo "Last Year \n";
// creates a list for last year
for ($row = 0; $row < count($toList); $row++)
{
echo $toList[$row] . " had " . $lastYear[$row] . "\n";
}
// randomly mixes up the to the toList
shuffle($toList);
echo "This Year \n";
// pairs the multidimensional array 1 index at a time
for ($row = 0; $row < count($array); $row++)
{
for ($col = 0; $col < 2; $col++)
{
// if it exists then print it out
if (isset($array[$row][$col]))
{
// if the toList index is the same person (as in $array), or already paired (in $that array), or a spouse (in $array), or the same person as last year - RESHUFFLE
while ($array[$row][$col] == $toList[$row] or in_array($toList[$row], $that) or in_array($toList[$row],$array[$row]) or $toList[$row] == $lastYear[$row])
{
// if it takes more then 200 Shuffles - BREAK
if ($n > 200)
{
echo "I'm Broke!! \n";
exit;
}
shuffle($toList);
$n++;
}
// once you find a match, add it to $that array and move on
$that[] = $toList[$row];
echo $array[$row][$col] . " has " . $toList[$row] . "\n";
}
}
}
我在这里和这里找到了类似的解决方案,但它们的条件与我不同。也可能有更好的方法来处理错误,但这可以在几次重新运行后完成。
我的问题是,有时它与您去年的同一个人配对(最常见的是最后一个结果与两年相同)。我的 while 循环有什么问题?
我假设以下 while 语句:
$toList[$row] == $lastYear[$row]
没有被解释成我想要的样子。但这在理论上似乎是正确的。