You are here: Home / Topics / How to get random value from an array in PHP

How to get random value from an array in PHP

Filed under: PHP on 2023-07-02 21:14:59

In this tutorial, we are going to see how to get random value from an array in PHP. You can use the PHP shuffle() function to randomly shuffle the order of elements or values in an array. The shuffle() function returns FALSE on failure.
 

How to get random value from an array in PHP
 

<?php
 $nbrs = array(1, 2, 3, 4, 5, 6);
  
 // Randomize the order of elements in the array 
 shuffle($nbrs);
 foreach ($nbrs as $value){
   echo "$value" . "<br>";
 }
?>
 

Output:

6
2
5
3
4
1


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.