PHP array_rand() Function

What is PHP array_rand() Function?

If you want to randomly get one or more keys (or indices) from an array, use PHP array_rand() function. With these keys you can select the corresponding element values.

Syntax:

array_rand(array, number)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

array (Required): This is the array which the function operates on. Check example 1.

number (Optional): This is a number which must be greater than 0 and less than or equal to the array length. Check example 2. Default value is one. So, if you don’t mention this parameter, the function returns one key. Check example 1.

Return Values:

The function returns-

  • A key number – if the function returns only one element. Check example 1.
  • An array of keys – if the function returns more than one element. Check example 2.

Examples:

Example 1:

<?php
$languages = ["PHP", "MySQL", "JavaScript", "Python", "Java"];
echo "A random array key is: " . array_rand($languages);
?>

Output:

A random array key is: 3

Explanation:

The array_rand() function returns one random key which is 3 here. You may get different one when you run this example.

Example 2:

<pre>
<?php
$languages = ["PHP", "MySQL", "JavaScript", "Python", "Java"];
echo "Two random generated keys are-" . "<br />";
print_r(array_rand($languages, 2));
?>
</pre>

Output:

Two random generated keys are-
Array
(
    [0] => 0
    [1] => 2
)

Errors/Exceptions:

  • If the array is empty, the function returns ValueError. Check example 3.
    <?php
    $languages = [];
    echo array_rand($languages);
    ?>
    
  • If the second parameter is out of range meaning either less than 1 or greater the number of the element of the array, the function returns ValueError. Check example 4.
    <pre>
    <?php
    $languages = ["PHP", "MySQL", "JavaScript", "Python", "Java"];
    print_r(array_rand($languages, -1));
    ?>
    </pre>
    

Notes on array_rand() Function:

  • When the function returns multiple keys, the function maintains the key orders presents in the array. Check example 2.

Best Practices on array_rand() Function:

  • In case you want to pick all the array element in random order, use shuffle() function instead of the array_rand() function to get faster output.

Difference between array_rand() and shuffle() function:

  • array_rand() function picks elements randomly from the array. On the other hand, shuffle() function randomize the order of the elements in the array, and then returns the elements.
  • After running the array_rand() function the order of elements in the original array don’t change. On the other hand, shuffle() function does change the order of the element in the original array.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP array_rand() Function

array_rand() is very useful function when you want to pick one or more keys from an array. It is one of many useful built-in array functions.

Reference:

https://www.php.net/manual/en/function.array-rand.php