Php randomly extracts one or more units from the array

Source: Internet
Author: User
Array_rand () is used to randomly extract one or more units from an array. it is quite useful to retrieve one or more random units from an array, it accepts input as an input array and an optional parameter num_req, specifying you... array_rand () is used to randomly extract one or more units from an array. it is quite useful to retrieve one or more random units from an array, it accepts input as an input array and an optional parameter num_req, specifying the number of units you want to retrieve-if not specified, the default value is 1. if you only retrieve one, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned.

 

Array_rand () definition and usage

The array_rand () function randomly selects one or more elements from the array and returns the result. the second parameter is used to determine the elements to be selected. if more than one element is selected, an array containing a random key name is returned. Otherwise, the key name of the element is returned.

Note:From php 4.2.0, the random number generator is no longer needed to be planted using the srand () or mt_srand () function, which has been automatically completed.

Syntax:Array_rand (array, number)

Parameter description

Array required, specifying the input array parameters.

Number (optional) the default value is 1, which specifies the number of random elements returned.

The PHP instance code is as follows:

 "dog","b"=>"cat","c"=>"horse"); print_r(array_rand($a,1));  

Permanent link:

Reprint at will! Include the article address.

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.