PHP array_rand function

Last Updated: February 14, 2022

array_rand – get one or more random keys from the array

Syntax

 array_rand(array $array, int $num = 1): int|string|array

Supports (PHP 4, PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array Required. target array
num Required. number of keys

Return Value

returns the key-value if you select num=1

returns array of keys if you select num>1

Example

<?php
$input = array("one", "two","three");
print_r(array_rand($input, 2));

?>

Output

Array ( [0] => 0 [1] => 1 ) 
Test