PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<array_pusharray_reduce>
Last updated: Mon, 16 Jul 2012

array_rand

(PHP 4, PHP 5)

array_rand --  从数组中随机取出一个或多个单元

说明

mixed array_rand ( array input [, int num_req] )

array_rand() 在你想从数组中取出一个或多个随机的单元时相当有用。它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1。

如果你只取出一个,array_rand() 返回一个随机单元的键名,否则就返回一个包含随机键名的数组。这样你就可以随机从数组中取出键名和值。

注: 自 PHP 4.2.0 起,不再需要用 srand()mt_srand() 函数给随机数生成器播种,现已自动完成。

例子 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";
?>

参见 shuffle()




<array_pusharray_reduce>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST