Php-Arrays function-array_rand-randomly retrieve one or more units from the array

Source: Internet
Author: User

Array_rand () randomly retrieves one or more units from the array.

[Function]
This function returns the specified number of array elements in an array.
If only one function is retrieved, the key name of a random unit is returned. Otherwise, an array containing a random key name is returned.
In this way, the keys and values can be randomly retrieved from the array.
Scope of use]
Php4, php5.
[Use]
Mixed array_rand (array input [. int num_req])
The return value is a hybrid array, and the input parameter is the original array to be retrieved.
The optional parameter num_req indicates the number of elements to be retrieved.
[Example]
[Php]
<? Php
Srand (float) microtime () * 10000000 );
$ Input = array ("Neo", "Morpheus", "Trinity", "Cypher", "");
Var_dump (array_rand ($ input, 2 ));
/*
Array (2 ){
[0] =>
Int (0)
[1] =>
Int (4)
}
*/

 


Taken from zuodefeng's note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.