This question already has answers here:

Using PHP, randomly pair up group of items, not pairing any with itself, no direct pairings
(7个答案)
示例数组:
$player = array("lawrence","joey","jason","joel","bianca","paulo","albert");

我要将数组与自身匹配,以便它从自身获取值而不获取重复值:
"lawrence" => "lawrence"; //must not contain itself
"joey" => "lawrence"; //must not assign value already assigned to that value

简而言之:
就像交换礼物的算法

最佳答案

$player = array("lawrence","joey","jason","joel","bianca","paulo","albert");
shuffle($player);
$player2 = $player;
array_unshift($player2, array_pop($player2));

$combined = array_combine($player, $player2);

洗牌它,把它转1来创建保证唯一的匹配,重新组合。

07-25 20:21