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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解php从数组中随机抽取一些元素的代码的相关内容吗,在本文为您仔细讲解随机抽取一些元素的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:随机抽取,元素,下面大家一起来学习吧。
复制代码 代码如下:

<?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;
?>

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们