Array_rand () randomly extracts one or more cells from an array

Source: Internet
Author: User

Array_rand ()- random extraction of one or more cells from an array

Mixed Array_rand Array $input $num _req = 1])

Extracts one or more random cells from an array and returns one or more keys for a random entry.

Parameters
input:

The input array. Necessary. Specifies the array.

num_req:

Indicates how many units you want to take out of the unit. Optional. Specifies how many random key names to return. If the number specified exceeds the number in the array, a E_WARNING level of error will occur.

Example 1

Returns a random key from the array:

<? PHP $a=array("a" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"); Print_r (array_rand($a, 1));? >
Example 2

Returns an array containing a random string key name:

<? PHP $a=array("a" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"); Print_r (array_rand($a, 2));? >

One random result:

Array

Two random results:

Array

Official Website Example:

Example #1 Array_rand () example

<? PHP $input Array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand _keys Array_rand ($input, 2); Echo $input [$rand _keys[0]]. "\ n"; Echo $input [$rand _keys[1]]. "\ n";? >

Array_rand () randomly extracts one or more cells from an array

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.