Home php教程 php手册 php从数组中随机抽取一个或多个单元办法

php从数组中随机抽取一个或多个单元办法

Jun 02, 2016 am 09:14 AM

从数组中随机抽取一个或多个单元办法,我们会用到array_rand (),想从数组中取出一个或多个随机的单元时相当有用,它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1,如果你只取出一个,array_rand() 返回一个随机单元的键名,否则就返回一个包含随机键名的数组.

<?php 
	srand((float) 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"; 
	 
	 
Copy after login

array_rand() 定义和用法

array_rand() 函数从数组中随机选出一个或多个元素,并返回.第二个参数用来确定要选出几个元素,如果选出的元素不止一个,则返回包含随机键名的数组,否则返回该元素的键名.

注释:自 php 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已被自动完成.

语法:array_rand(array,number)

参数 描述

array 必需,规定输入的数组参数.

number 可选,默认是 1,规定返回多少个随机的元素.

PHP实例代码如下:

<?php 
	$a=array("a"=>"dog","b"=>"cat","c"=>"horse"); 
	print_r(array_rand($a,1)); 
	 
	
Copy after login

永久链接:

转载随意!带上文章地址吧。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)