当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > array_rand() 函数

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 array_rand() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 450 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.
array_rand()函数用于随机返回数组中的一个键;如果你希望随机返回多个键,那么他将以数组的形式返回这些键。

Syntax
语法

array_rand(array,number)

Parameter参数 Description描述
array Required. Specifies an array
必要参数。指定一个数组
number Optional. Default 1. Specifies how many random keys to return
可选参数。默认值为1。指定随机返回的键数量


Example 1
案例1

<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_rand($a,1));
?>

The output of the code above could be:
上述代码将输出下面的结果:

b


Example 2
案例2

An array with string keys:
数组中包含字符串键的案例:

<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_rand($a,2));
?>

The output of the code above could be:
上述代码将输出下面的结果:

Array ( [0] => c [1] => b )

评论 (0) All

登陆 | 还没注册?