Web | This Site

Learning PHP
Advanced PHP
PHP Function Reference
PHP Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# PHP Function array_rand()

## Syntax

array_rand ( $input [, $num_req] );

## Definition and Usage

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

## Paramters

Parameter | Description
--- | ---
array | Required. Specifies an array.
num_req | OPtional. 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

Advertisement

Online Image Processing

Indian Baby Names