zoukankan      html  css  js  c++  java
  • php从数组中随机抽取一些元素

    <?php
    class getValues {
       function __construct($inputArray){
            $this->inputArray = $inputArray;
       }
        //该函数此处没用到
        public function inputValue($inputArray) {
            $this->inputArray = $inputArray;
        }
        public function getValue($number) {
            $this->number = $number;
            for($i = 0; $i < $this->number; $i ++) {
              $count = count ( $this->inputArray ) - 1 - $i;
              $index = rand ( 0, $count);
              $getArray [$i] = $this->inputArray [$index];
              $temp = $this->inputArray [$count];
              $this->inputArray [$count] = $this->inputArray [$index];
              $this->inputArray [$index] = $temp;
            }
            //asort ( $getArray ); // 从小到大排序,根据需要修改
            return $getArray;
        }
    }
    
    //测试代码
    $keywords  = array(
            "我们",
            "你们",
            "他们"
    );
    $getValue=new getValues($keywords);
    $key = $getValue->getValue(1);//从数组中随机抽取一个元素
    ?>

    推荐一个自己业余时间开发的网盘搜索引擎,360盘搜www.360panso.com
  • 相关阅读:
    文件操作类File
    文件流操作
    自动登录和解/加密
    redis---set类型常用命令
    div
    css2
    css-id选择器
    table标签
    段落标签-换行标签
    input-form-select-a-img-ul-dl标签
  • 原文地址:https://www.cnblogs.com/eczhou/p/2751254.html
Copyright © 2011-2022 走看看