组合到数组的函数被调用
array_combine
. 你可以看看
here
,并这样使用:
<?php
function checkSame ($a, $b) {
foreach (array_combine($a,$b) as $key => $value)
if ($key===$value) return true;
return false;
}
$array_give = array('Paul & Becky', 'Keith & Jackie', 'Dave & Lauren', 'Ashley & Jeric', 'Rob & Savannah');
$array_receive = array('Paul & Becky', 'Keith & Jackie', 'Dave & Lauren', 'Ashley & Jeric', 'Rob & Savannah');
while (checkSame($array_give, $array_receive)) shuffle($array_receive);
foreach( array_combine($array_give, $array_receive) as $give => $receiving)
{
echo $give. " give to ".$receiving . "<br>";
}
我的想法是防止自我馈赠,只需继续洗牌数组,直到没有人分配给自己。从理论上讲,这可能会永远持续下去,但我认为在大多数实际应用中,它会很快停止。