How to get a random value from an array in php

Updated: 31-Jul-2022 | Tags: PHP Tutorials | Views: 211


Hello everyone, in this tutorial we are going to see how to get a random value from an array.
Let's say you have an array full of usernames and you want to pick a random user as a winner for your give away. Let's see how we do this.

First of all we need an array of users like the one below.

$users = ["Bob", "Peter", "Dennis", "Sophia", "Tania"];

The $users array is an indexed array.

Let's see how php sees the $users array.

$users (
  0 => 'Bob',
  1 => 'Peter',
  2 => 'Dennis',
  3 => 'Sophia',
  4 => 'Tania',

We see that php is assigning an index on every value starting from the number zero.
PHP arrays are zero based, that means the the first value has an index of zero.
The second value has an index of one, and so on ...

Now that we know this, we can pick a random index, to get a random username.
PHP has a function called array_rand() which picks a random index and returns it. Let's see it.

The array_rand function

The array_rand() function takes two arguments. The first argument is the array we want to use, and the second argument is an integer which tells the function how many random indexes to return.
Let's see how we use the array_rand function.

$draw = array_rand($users, 1);
  • We pass in the first argument the $users array, and in the second argument we have the number of the random indexes we want the function to return, in our case just one.
    And we store the returned index in the $draw variable.

Now we can use the $draw variable to get the random user like this ...

$winner = $users[$draw];

Now the $winner variable is holding the random picked username, and we can echo it out. For example, Dennis.

echo $winner; // ex. Dennis

And this is how we get a random value from an array.

Now let's see how we can get more than one random users.

Get more than one random values from an array

We are going to use again the array_rand() function but this time in the second argument will have the number three.

$indexes = array_rand($users, 3);

This time the array_rand() function returns an array that holds the three random picked indexes.
Let me say, that the array_rand() function will not pick the same index twice in one call. That means that there is no way that we get the same username twice. That is good.

So to get access to the random picked usernames, we have to loop through the $indexes array, and use the index on the $users array, which will give us the username.

foreach ($indexes as $index) {
	echo $users[$index];		
This will echo out something like: Dennis, Sophia, Bob.

Now this is how the array_rand() works, i hope it was clearly enough explained.

The shuffle function

Now we can do another thing to get a random value from an array, and that is using the shuffle() function.

Let's use again the same $users array.

$users = ["Bob", "Peter", "Dennis", "Sophia", "Tania"];

To use the function we have to say ....


The function takes as an argument an array, and returns true on success, or false on failure.

The shuffle() function will remove any existing key in the $users array, and will assign to each value a new, random one.
So to pick a random user we can target the first index which is zero.
Every time the function runs we will get a random username.

echo $users[0];

If we want two random users from the array we can use the index one [1], and so on ...

echo $users[0];
echo $users[1];
The above code will return something like this: Bob, Tania

Now, it is easier to use the shuffle() function to get a random value from an array, but have in mind that the $users array is now reordered, and will stay so until the end of the file's execution. So if you have something else to do with the $users array you might need to have this in your mind.

I hope you liked this quick tutorial on how to get a random value from an array.


We saw how to use the php array_rand() and the shuffle() functions to target a random item from an array.

Last words

Thanks for reading, i hope you find the article helpful.
Please leave a comment if you find any error's, so i can update the page with the correct code.

Buy me a coffee

If you like to say thanks, you can buy me a coffee.

Buy me a coffee with paypal

Comment section

You can leave a comment, it will help me a lot.

Or you can just say hi. 😉

Tutorial Categories