php從數(shù)組中隨機抽取一些元素的代碼
更新時間:2012年11月05日 17:16:15 作者:
php從數(shù)組中隨機抽取一些元素的代碼,需要的朋友可以參考下
復制代碼 代碼如下:
<?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 ); // 從小到大排序,根據(jù)需要修改
return $getArray;
}
}
//測試代碼
$keywords = array(
"我們",
"你們",
"他們"
);
$getValue=new getValues();
$getValue->inputValue($keywords);
$key = $getValue->getValue(1);//從數(shù)組中隨機抽取一個元素
echo $key;
?>
相關文章
php + ajax 實現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
這篇文章主要介紹了php + ajax 實現(xiàn)的寫入數(shù)據(jù)庫操作,結合實例形式分析了php + ajax 寫入數(shù)據(jù)庫基本原理、操作技巧與相關使用注意事項,需要的朋友可以參考下2020-05-05
php數(shù)組索引的Key加引號和不加引號的區(qū)別
這篇文章主要介紹了php數(shù)組索引的Key加引號和不加引號的區(qū)別,加引號和不加引號是有嚴重的區(qū)別的,需要的朋友可以參考下2014-08-08

