How to Get Random Value from an Array in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get First Element of Array in PHP?
- How to Convert String into Array in PHP?
- How to Remove Last Element from Array in PHP?
- PHP Get All Array Keys Starting with Certain String Example
- PHP Multidimensional Array Search By Value Example
- How to Remove Multiple Keys from PHP Array?
- How to Convert Array to JSON in PHP?
- PHP - How to Reindex Array Key After Unset Key?
- How to Remove Specific Element by Value from Array in PHP?
- How to Get Maximum Key Value of Array in PHP?
- How to Remove Empty Values from Array in PHP?
- How to Add Prefix in Each Key of PHP Array?
- How to Get Minimum Key Value of Array in PHP?
- How to Remove Null Values from Array in PHP?