How to Get Random Value from an Array in PHP?

By Hardik Savani November 5, 2023 Category : PHP

Hello,

In this article, we will cover php array get random value. I explained simply step by step how to get random element from array in php. This tutorial will give you a simple example of how to get random value from array in php. you'll learn php get random value from array.

we will use array_rand() function to get random element of an array in php. so, let's see the simple code of how get random element from array in php.

Example:

index.php

<?php

$myArray = ["One", "Two", "Three", "Four"];

$randElem = $myArray[array_rand($myArray)];

var_dump($randElem);

?>

Output:

string(5) "Three"

I hope it can help you...

Tags :
Shares