I have an array displaying results as below:
Array
(
[0] => 71
[1] => 56
[2] => 64
[3] => 82
[4] => 90
[5] => 80
[6] => 65
[7] => 62
[8] => 14
[9] => 3
)
My code is:
while($row = mysql_fetch_assoc($result))
{
$show[] = $row['number'];
}
echo '<pre>'; print_r($show);
How can I randomize the array contents and display them as 5 digits only e:g 71 64 14 80 82
$rand5 = array_rand(array_flip($show), 5);
// we need array_flip because array_rand returns the keys
http://php.net/array_rand
http://php.net/array_flip
I would go with shuffle to randomize the arrays, as per getting the 5 elements of teh array, it really depends on your need, what do you exactly want to accomplish, getting the first 5 elements, the last ones, or some other logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With