php get two different random array elements
arrays, php, random
Solution
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]);
}
Problem
From an array ``` $my_array = array('a','b','c','d','e'); ``` I want to get two DIFFERENT random elements. 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; } ``` 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