How To Random The Order Of A List In PHP
I have a list of non-repetitive numbers; i need to display all these numbers in a random manner. From http://www.w3schools.com/php/func_array_rand.asp I learn a few approaches with
Solution 1:
The shuffle function will randomize the order of the elements in the array for you.
$a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle($a);
echo implode('<br>', $a);
Solution 2:
The PHP you're actually looking for is:
$a = array(1,2,3,4,5,6,7,8,9,10);
shuffle($a);
foreach($a as $n){
echo "$n<br>";
}
array_rand
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
shuffle
shuffles (randomizes the order of the elements in) an array
Solution 3:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array(1,2,3,4,5,6,7,8,9,10);
print_r($a);
shuffle($a);
echo "<br/>";
print_r($a);
?>
</body>
</html>
Solution 4:
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = range(1, 10);
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number<br>";
}
?>
</body>
</html>
Post a Comment for "How To Random The Order Of A List In PHP"