php从数组中随机抽取一些元素的代码

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
class getValues {
public function inputValue($inputArray) {
$this->inputArray = $inputArray;
}
public function getValue($number) {
$this->number = $number;
for($i = 0; $i < $this->number; $i ++) {
$index = rand ( 0, count ( $this->inputArray ) - 1 - $i );
$getArray [$i] = $this->inputArray [$index];
unset ( $this->inputArray [$index] );
for($k = $index; $k < count ( $this->inputArray ) - 1; $k ++) {
$this->inputArray [$k] = $this->inputArray [$k + 1];
}
}
//asort ( $getArray ); // 从小到大排序,根据需要修改
return $getArray;
}
}

//测试代码
$keywords = array(
"我们",
"你们",
"他们"
);
$getValue=new getValues();
$getValue->inputValue($keywords);
$key = $getValue->getValue(1);//从数组中随机抽取一个元素
echo $key;
?>

相关文章

标准PHP的AES加密算法类

分享一个标准PHP的AES加密算法类,其中mcrypt_get_block_size('rijndael-128', 'ecb');,如果在不明白原理的情况下比较容易搞错,可以通过mcr...

使用PHP反射机制来构造"CREATE TABLE"的sql语句

反射是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对...

PHP中遍历stdclass object的实现代码

需要操作的数据: 复制代码 代码如下: $test =Array ( [0] => stdClass Object ( [tags] => 最快的车,Bloodhound,S...

使用php将某个目录下面的所有文件罗列出来的方法详解

直接给源代码了:复制代码 代码如下:$current_dir = 'E:/temp/';$dir = opendir($current_dir);echo "direcotry list...

试用php中oci8扩展

给大家分享个php操作Oracle的操作类 Oracle_db.class.php <?php class Oracle_db{ public $link; p...