php从数组随机抽取一些元素的解决办法
内容摘要
这篇文章主要为大家详细介绍了php从数组随机抽取一些元素的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
代码如下:
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
代码如下:
文章正文
这篇文章主要为大家详细介绍了php从数组随机抽取一些元素的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <code> <?php /* php教程 www.512Pic.com */ 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 ; ?> </code> |
注:关于php从数组随机抽取一些元素的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释