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]);
 }