As far as I know array_rand()
can only grab a radnom array from an array like this:
$array = array( \'apple\', \'orange\', \'banana\' );
$two_ra
Just shuffle and slice 2:
shuffle($array);
$rand_values = array_slice($array, 0, 2);
First, this line: $rand_values[] .= $array[$key];
is wrong. The .=
operator is to join strings, to add a value to the end of the array, you just need $rand_values[] = $array[$key];
.
If you don't care about the keys, just use array_values
function to "dump" the keys.
$array = array('a' => 'orange', 'c' => 'banana', 'b' => 'peach');
$two_random_items = array_rand(array_values($array) , 2 );
array_values will strip down the keys, and will return an array with the values (the keys will become 0, 1, 2...)