PHP得到两个不同的随机数组元素 [英] php get two different random array elements

查看:192
本文介绍了PHP得到两个不同的随机数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数组

 $my_array = array('a','b','c','d','e');

我要得到两个不同的随机元素。

I want to get two DIFFERENT random elements.

通过以​​下code:

With the following code:

 for ($i=0; $i<2; $i++) {
    $random = array_rand($my_array);  # one random array element number
    $get_it = $my_array[$random];    # get the letter from the array
    echo $get_it;
 }

也能够获得相同的信两次。我需要prevent这一点。我想总是得到两个不同的数组元素。有人能告诉我该怎么做?
谢谢

it is possible to get two times the same letter. I need to prevent this. I want to get always two different array elements. Can somebody tell me how to do that? Thanks

推荐答案

您可以随时删除您所选择的第一次圆的元素,那么你就不会再捡起来。如果你不想修改数组创建副本。

You could always remove the element that you selected the first time round, then you wouldn't pick it again. If you don't want to modify the array create a copy.

 for ($i=0; $i<2; $i++) {
    $random = array_rand($my_array);  # one random array element number
    $get_it = $my_array[$random];    # get the letter from the array
    echo $get_it;

    unset($my_array[$random]);
 }

这篇关于PHP得到两个不同的随机数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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