Q:

How to randomize the order of an array in PHP

0

How to randomize the order of an array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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>";
}
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to compare two array values in PHP... >>
<< How to remove duplicate values from an array in PH...