Copyright © tutorialspoint.com

PHP Function array_rand()

previous
Advertisements

Syntax

array_rand ( $input [, $num_req] );

Definition and Usage

This function pick one or more random entries out of an array.

Paramters

ParameterDescription
arrayRequired. Specifies an array.
num_reqOPtional. Specifies how many entries you want to pick - if not specified, it defaults to 1.

Return Values

If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries.

Example

Try out following example:

<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_rand($array));
?> 

This will produce following result, This result will vary every time you will execute script:

b
previous

Copyright © tutorialspoint.com