php從數(shù)組中隨機(jī)抽取一些元素的代碼
更新時間:2012年11月05日 17:16:15 作者:
php從數(shù)組中隨機(jī)抽取一些元素的代碼,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<?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ù)組中隨機(jī)抽取一個元素
echo $key;
?>
您可能感興趣的文章:
- php將字符串隨機(jī)分割成不同長度數(shù)組的方法
- php對數(shù)組內(nèi)元素進(jìn)行隨機(jī)調(diào)換的方法
- php中實(shí)現(xiàn)獲取隨機(jī)數(shù)組列表的自定義函數(shù)
- php生成不重復(fù)隨機(jī)數(shù)、數(shù)組的4種方法分享
- php獲取隨機(jī)數(shù)組列表的方法
- php中通過數(shù)組進(jìn)行高效隨機(jī)抽取指定條記錄的算法
- php數(shù)組函數(shù)序列 之shuffle()和array_rand() 隨機(jī)函數(shù)使用介紹
- php數(shù)組隨機(jī)排序?qū)崿F(xiàn)方法
相關(guān)文章
php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
這篇文章主要介紹了php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了php + ajax 寫入數(shù)據(jù)庫基本原理、操作技巧與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2020-05-05
php數(shù)組索引的Key加引號和不加引號的區(qū)別
這篇文章主要介紹了php數(shù)組索引的Key加引號和不加引號的區(qū)別,加引號和不加引號是有嚴(yán)重的區(qū)別的,需要的朋友可以參考下2014-08-08
php實(shí)現(xiàn)webservice實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)webservice的方法,以實(shí)例形式講述了webservice的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2014-11-11

