Please note, this is a STATIC archive of website www.tutorialrepublic.com from 10 Sep 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to randomize the order of an array in PHP

Topic: PHP / MySQLPrev|Next

Answer: Use the PHP shuffle() function

You can use the PHP shuffle() function to randomly shuffle the order of the elements or values in an array. The shuffle() function returns FALSE on failure.

Let's try out the following example to understand how this function basically works:

<?php
// Creating an array containing a range of elements
$numbers = range(1, 10);
 
// Randomize the order of array items
shuffle($numbers);
foreach ($numbers as $value){
    echo "$value" . "<br>";
}
?>

Related FAQ

Here are some more FAQ related to this topic:

Advertisements
Bootstrap UI Design Templates