PHP将数组随机匹配到自身中 示例阵列:

PHP将数组随机匹配到自身中 示例阵列:,php,arrays,algorithm,list,Php,Arrays,Algorithm,List,我希望将数组与自身匹配,以便它从自身获取值,而不获取重复值: "lawrence" => "lawrence"; //must not contain itself "joey" => "lawrence"; //must not assign value already assigned to that value 简言之: 这就像是交换礼物的算法我发现了这篇帖子: 上面的答案看起来很好: 这给了我:

我希望将数组与自身匹配,以便它从自身获取值,而不获取重复值:

"lawrence" => "lawrence"; //must not contain itself
"joey" => "lawrence"; //must not assign value already assigned to that value
简言之: 这就像是交换礼物的算法

我发现了这篇帖子:

上面的答案看起来很好:

这给了我:

Array
(
    [lawrence] => joel
    [joel] => joey
    [joey] => jason
    [jason] => paulo
    [paulo] => albert
    [albert] => bianca
    [bianca] => lawrence
)
你可能需要检查一下,看看对方是否在你的礼物交换中选择了自己

$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以创建保证唯一的匹配,重新组合。

我想将数组与自身匹配,这样它就可以从自身获得一个值,而不会获得重复的值:你能用英语说这个吗?你想。。。要从你的数组中随机选择值,而不是让它重复它选择的值吗?yes Preciscely@glowy.penguin另外,所选值不能再次选择我认为目标就像一个外部连接,玩家应该在这里配对。。。除了两个不起作用的例子之外,你能举个例子说明什么是有效的吗?和@HankyPanky-给OP一个本周最佳英语新词的分数。随机是我见过的最酷的东西。交换礼物算法你的意思是像一个秘密的圣诞老人?这是我想到的外部连接的想法吗?限制每个阵列成员一个?我不认为这是相同的想法。这只是将阵列旋转一个位置并将其重新组合。这很好,但它有一个模式,必须在其上添加随机效果以便像一个秘密的圣诞老人?@Lawrence唯一的模式是阵列中有一个顺序a>b,b>c,a和b或b和c之间没有模式。在联合收割机后再次洗牌,以移除链条。@downy.penguin谢谢,您最近回答的问题也是working@LawrenceBoadilla我看到另一个人已经为你的问题提供了解决方案,绿色的复选标记,但你似乎仍然在评论中讨论问题。。。有什么我可以添加到这段代码中来解决你的问题吗?@事实上,deceze的算法已经很好了,我只是一开始没有得到它。我以为它用的是rot13算法。
Array
(
    [lawrence] => joel
    [joel] => joey
    [joey] => jason
    [jason] => paulo
    [paulo] => albert
    [albert] => bianca
    [bianca] => lawrence
)
$player = array("lawrence","joey","jason","joel","bianca","paulo","albert");
shuffle($player);
$player2 = $player;
array_unshift($player2, array_pop($player2));

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