• Post category:PHP

How to Randomize Array value PHP

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:

shuffle array values in php

  1. Here, we have created an array variable “$rainbow” and assigned some values.
  2. 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.
  3. Then print the array, and see the output. Values will be shuffled successfully.

Leave a Reply