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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM