加入收藏 | 设为首页 | 会员中心 | 我要投稿 珠海站长网 (https://www.0756zz.cn/)- 云服务器、边缘计算、基础存储、云计算、中间件!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2022-07-24 10:54:01 所属栏目:PHP教程 来源:互联网
导读:php从数组中随机抽取一些元素代码大家可参考一下. 实例代码如下: ?php class getValues { public function inputValue($inputArray) { $this-inputArray = $inputArray; } public function getValue($number) { $this-number = $number; for($i = 0; $i $thi
  php从数组中随机抽取一些元素代码大家可参考一下.
 
  实例代码如下:
 
  <?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);//从数组中随机抽取一个元素
  ?>
   

(编辑:珠海站长网)

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

    热点阅读