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

yipeiwu_com6年前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 exif扩展方法开启详解

服务器配置说明: 1.在php.ini文件中找到;extension=php_exif.dll,去掉前面的分号 2.在php.ini文件中找到;extension=php_mbstrin...

php生成图片缩略图的方法

本文实例讲述了php生成图片缩略图的方法。分享给大家供大家参考。具体如下: 这里需要用到GD2 library function make_thumb($src,$dest,$des...

关于PHP中协程和阻塞的一些理解与思考

前言 本文主要给大家介绍了关于PHP中协程和阻塞的理解与思考,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 进程、线程、协程 关于进程、线程、协程,有非常详细和丰富的...

php通过array_merge()函数合并关联和非关联数组的方法

本文实例讲述了php通过array_merge()函数合并关联和非关联数组的方法。分享给大家供大家参考。具体分析如下: array_merge()是一个用于合并数组的php函数,后一个数...

PHP 调试工具Debug Tools

PHP 调试工具Debug Tools

发现了个调试PHP源码的好同志: PHP DEBUG TOOLS 其项目地址: http://freshmeat.net/projects/php-debug-tools/ 文件下载地址...