Get Random Value From Array With Laravel Helper.
Hello Guys,
Today we will about another laravel helper function. This function is also related to the array helper. Suppose we need to fetch any random value from the array list.
We can get this by using the Laravel array helper. We will use the Arr::random function to get the random value from the array list.
Arr::random
This function will return a single random value from the string.
use Illuminate\Support\Arr;
$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);
// 4 - (retrieved randomly)
If you want to set the number of values to return. You can pass the second parameter as an optional value.
Example with the second parameter.
use Illuminate\Support\Arr;
$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);
// 4 - (retrieved randomly)
I hope this post is helpful to you.
Please share the post with friends.
Follow Social:
Facebook
Twitter
Youtube
Thanks
1027