PHP:随机排列,所以没有价值就像是记者1 [英] PHP: Shuffle array, so no value is like the correspondent one

查看:138
本文介绍了PHP:随机排列,所以没有价值就像是记者1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有与名称的数组,例如:

There is an array with names, for example:

$ donalds_nephews =阵列('休伊','杜威','路易');

$donalds_nephews = array('Huey', 'Dewey', 'Louie');

array
(
    [0] => Huey
    [1] => Dewey
    [2] => Louie
)

我要洗牌此阵,但要确保没有原始数组的值具有相同的密钥的洗牌之一。

I want to shuffle this array, but ensure that no value of the original array has the same key as the shuffled one.

$donalds_nephews_shuffled = shuffle($donalds_nephews);

这会导致6种可能的排列:

This could result in 6 possible permutations:


  1. 休伊,杜威,路易

  2. 休伊,路易,杜威

  3. 杜威,路易,休伊

  4. 杜威,休伊,路易

  5. 路易,杜威,休伊

  6. 路易,休伊,杜威

  1. Huey, Dewey, Louie
  2. Huey, Louie, Dewey
  3. Dewey, Louie, Huey
  4. Dewey, Huey, Louie
  5. Louie, Dewey, Huey
  6. Louie, Huey, Dewey

1,第2,第4和第5切不可结果。

1st, 2nd, 4th and 5th must not be the result.

什么是这样做的最佳方式?它是神秘的圣诞老人。

What's the best way to do so? It's for Secret Santa.

推荐答案

洗牌原数组,然后做一个副本,并在转移所有条目之一,再搭配两个重新走到一起,让您的匹配。

Shuffle the original array, then make a copy of it and shift all entries one over, then match the two back together to get your matches.

这篇关于PHP:随机排列,所以没有价值就像是记者1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆