PHP array_rand() 函数

PHP 教程 · 2019-01-22 12:12:30

实例

返回一个包含随机键名的数组:

<?php $a=array("red","green","blue","yellow","brown"); $random_keys=array_rand($a,3); echo $a[$random_keys[0]]."<br>"; echo $a[$random_keys[1]]."<br>"; echo $a[$random_keys[2]]; ?>

定义和用法

array_rand() 函数返回数组中的一个随机键名,或者如果指定函数返回键名不只一个,则返回一个包含随机键名的数组。

语法


array_rand(array,number)

参数 描述
array 必需。规定数组。
number 可选。规定返回多少个随机的元素。

技术细节

返回值: 返回数组中的一个随机键名,或者如果指定函数返回键名不只一个,则返回一个包含随机键名的数组。
PHP 版本: 4+
更新日志: 自 PHP 5.2.10 起,键名的结果数组不再ay of keys is no longer shuffled。

自 PHP 4.2.0 起,随机数生成器会自动播种。

更多实例

实例 1

返回数组中的一个随机键名:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,1));
?>

实例 2

返回一个包含随机字符串键名的数组:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,2));
?>

点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html

查看所有标签

Lighttpd

Lighttpd

Andre Bogus / Packt Publishing / 2008-10 / 39.99

This is your fast guide to getting started and getting inside the Lighttpd web server. Written from a developer's perspective, this book helps you understand Lighttpd, and get it set up as securely an......一起来看看 《Lighttpd》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具