Array function library: Array_rand

Source: Internet
Author: User
Keywords array_rand php Chinese function manual array function library
Tags aliyun array array function library array_rand function function library html http

Array_rand

(PHP4 >= 4.0.0)

Array_rand---&http://www.aliyun.com/zixun/aggregation/37954.html ">nbsp; Randomly select one or more values of an array

Syntax: mixed array_rand (array input [, int num_req])

Description:

Array_rand () is useful when you want to randomly select one or more of the values of an array, the argument num_reg is not necessary, it indicates that you want to select a few values out, if not specified, then its default value is 1.

If you select only one value, Array_rand () returns the index of the random value. Otherwise, it returns the index of the random value in the array and returns the array. This way you can not only pick random index values but also output the values of the array.

Don't forget to call Srand () to set the number of random seeds.

Example:

<?php

Srand (Double) microtime () * 10000000);

$input = Array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank");

$rand _keys = Array_rand ($input, 2);

Print $input [$rand _keys[0]]. " \ n ";

Print $input [$rand _keys[1]]. " \ n ";

?>

Related Article

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.