加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 20:25:12 所属栏目:PHP教程 来源:网络整理
导读:php从数组中随机抽取一些元素代码大家可参考一下. 实例代码如下: ?php ? class getValues{ public function inputValue( $inputArray ){ $this -inputArray= $inputArray ; } public function getValue( $number ){ $this -number= $number ; for ( $i =0; $

php从数组中随机抽取一些元素代码大家可参考一下.

实例代码如下:

  1. <?php 
  2. ?class getValues { 
  3.  
  4.     public function inputValue($inputArray) { 
  5.         $this->inputArray = $inputArray
  6.     } 
  7.     public function getValue($number) { 
  8.         $this->number = $number
  9.         for($i = 0; $i < $this->number; $i ++) { 
  10.             $index = rand ( 0, count ( $this->inputArray ) - 1 - $i ); 
  11.             $getArray [$i] = $this->inputArray [$index]; 
  12.             unset ( $this->inputArray [$index] ); 
  13.             for($k = $index$k < count ( $this->inputArray ) - 1; $k ++) { 
  14.                 $this->inputArray [$k] = $this->inputArray [$k + 1]; 
  15.             } 
  16.         } 
  17.         //asort ( $getArray ); // 从小到大排序,根据需要修改 
  18.         return $getArray
  19.     } 
  20. //测试代码 
  21. $keywords  = array
  22.         "我们"
  23.         "你们"
  24.         "他们" 
  25. ); 
  26. $getValue=new getValues(); 
  27. $getValue->inputValue($keywords); 
  28. $key = $getValue->getValue(1);//从数组中随机抽取一个元素 
  29. ?> 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读