以同样的方式打乱 2 个 php 数组
例如,我有这个:
$array['one'][0] = 0;
$array['one'][1] = 1;
$array['one'][2] = 2;
$array['one'][3] = 3;
$array['two'][0] = 00;
$array['two'][1] = 11;
$array['two'][2] = 22;
$array['two'][3] = 33;
我怎样才能将它们洗牌以获得类似的结果:
$array['one'][0] = 2;
$array['one'][1] = 1;
$array['one'][2] = 3;
$array['one'][3] = 0;
$array['two'][0] = 22;
$array['two'][1] = 11;
$array['two'][2] = 33;
$array['two'][3] = 00;
或任何其他随机顺序,但两者具有相同的“随机因子”?
例如,我希望将 $array['one'][0]
和 $array['two'][0]
进行混洗以获得 $array ['one'][x]
和 $array['two'][x]
(x
是随机键,但两个数组上的相同)。
I have this for example:
$array['one'][0] = 0;
$array['one'][1] = 1;
$array['one'][2] = 2;
$array['one'][3] = 3;
$array['two'][0] = 00;
$array['two'][1] = 11;
$array['two'][2] = 22;
$array['two'][3] = 33;
How can I shuffle them both to get something like:
$array['one'][0] = 2;
$array['one'][1] = 1;
$array['one'][2] = 3;
$array['one'][3] = 0;
$array['two'][0] = 22;
$array['two'][1] = 11;
$array['two'][2] = 33;
$array['two'][3] = 00;
Or any other random order, but having the same "random factor" in both?
For example, I want that $array['one'][0]
and $array['two'][0]
get shuffled to get $array['one'][x]
and $array['two'][x]
(x
being a random key, but the SAME on both arrays).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
像这样的东西可以工作。它与 Mimikry 的答案类似,只是即使数组一中碰巧有重复的值,这个答案也会起作用(这个答案不使用数组一的值作为临时数组的键)。
假设两个数组的大小相同。
Something like this could work. It's similar to Mimikry's answer except this one will work even if you happen to have duplicate values in array one (this one doesn't use values of array one as keys of the temporary array).
Assuming both arrays are of the same size.
希望我的理解是正确的。
对于上面的示例,此代码应该可以工作。但这非常hacky...
var_dump($array):
hopefully i get you right.
For your example above, this code should work. But it's pretty hacky...
var_dump($array):
恕我直言,最佳实践如下:
这是清晰的代码,并且应该很快。不需要像其他答案一样重新发明轮子。
The best practice, imho, is as follows:
This is clear code and should be fast. No need to reinvent the wheel like in some other answers.
这不是多维数组的示例,但它非常适合以相同的方式和随机播放对多个普通数组进行排序。也许它也可以适用于多维数组。它确实假设所有数组的长度相同。
这将为您提供新的数组,这些数组都以相同的随机方式排序。然后,您可以将旧数组设置为与新数组相同或保持它们不变。
This isn't an example with multidimensional arrays but it works great for sorting multiple normal arrays the same way and with shuffle. Maybe it could be adapted for multidimensional arrays too. It does assume all arrays are the same length.
This will give you new arrays which are all sorted the same random way. You could then set the old arrays equal to the new ones or keep them in-tact.
不确定您到底想做什么,但最好的选择可能是将数组 1 和数组 2 放入多维数组中,然后随机主数组。这将为您提供随机排列的值,但每个键中将包含每个数组的值。如果您能提供有关您正在做的事情的更多详细信息,我们可以为您提供更具体的答案。
Not sure exactly what you are trying to do, but your best bet is probably to put array 1 and array 2 into a multi-dimensional array and then random the primary array. That will give you a random arrangement of values, but within each key will be the values of each array. We could give you more specific answers if you can provide a bit more detail of exactly what you are doing.