教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 从数组中随机抽取一些元素的php代码

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

发布时间:2015-02-16   编辑:jiaochengji.com
从数组中随机抽取一些元素的php代码,供大家学习参考。

从数组中随机抽取一些元素的php代码,供大家学习参考。
 

复制代码 代码如下:

<?php
/**
* 随机抽取数组元素
* author: notuser
* 2012-12-29
*/
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代码
php生成随机产生六位数密码的代码
php数组函数之shuffle()与array_rand() 随机函数
JavaScript算法题之–随机数的生成
php从数组中随机抽取一些元素代码
PHP学习之array_rand()数组随机选择函数
JavaScript 数组随机抽取的代码(简洁好用)
Python random模块及用法
php抽奖小程序代码一例
有关c# 随机函数的实例详解

关键词: php数组  php随机数   
[关闭]
~ ~