Tutorials Point


  Learning PHP
  Advanced PHP
  PHP Function Reference
  PHP Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

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 Printer Friendly

Advertisements


  

Advertisements