Randomize Array PHP
In this article, we will learn how to randomize array values in php. To randomize or shuffle the array values, we need to use the shuffle() function. It is one of the PHP in-built functions. This function is used to Randomly rearranges the sequences of elements in an array. Let’s see the example of shuffle:
<?php
$rainbow = array("violet", "indigo", "blue", "green"," yellow", "orange","red");
shuffle($rainbow); //randomize array
print_r($rainbow);
?>
Output:
- Here, we have created an array variable “$rainbow” and assigned some values.
- If you want to shuffle the values in $rainbow, we need to use the shuffle() function and pass the array name $rainbow as an argument.
- Then print the array, and see the output. Values will be shuffled successfully.