时时商务社区
标题:
php从数组中随机抽取一些元素的代码
[打印本页]
作者:
yj1281
时间:
2018-2-14 08:27
[U]复制代码[/U] 代码如下:
inputArray = $inputArray;
}
public function getValue($number) {
$this->number = $number;
for($i = 0; $i number; $i ++) {
$index = rand ( 0, count ( $this->inputArray ) - 1 - $i );
$getArray [$i] = $this->inputArray [$index];
unset ( $this->inputArray [$index] );
for($k = $index; $k 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数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
php中通过数组进行高效随机抽取指定条记录的算法
php获取随机数组列表的方法
php生成不重复随机数、数组的4种方法分享
php中实现获取随机数组列表的自定义函数
php对数组内元素进行随机调换的方法
php将字符串随机分割成不同长度数组的方法
php数组随机排序实现方法
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2