| Viewed 13 times

How to return a random value from an array in laravel


How to return a random value from an array in laravel

1 Answer
Mohammad Qandeel
Answered
0

You can use the random() helper function to return a random value from an array in laravel

For Laravel developers, you can get a random value from an array.

If you want to return a random value from an array in laravel you can use the random() helper function which will return a random value.

This helper function can accept an optional second argument to specify the number of items to return and return a new array of random item. $items = Arr::random($array, 2);

Example of how to use random() helper function

use Illuminate\Support\Arr;

$array = [1, 2, 3, 4, 5];

$random = Arr::random($array);

Output

['0', '1', '2', '3', '4']

 


Related FAQ