這篇文章給大家介紹使用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);//從數組中隨機抽取一個元素
echo $key;
?>
關于使用php怎么從數組中隨機抽取元素就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
文章標題:使用php怎么從數組中隨機抽取元素-創新互聯
本文來源:http://vcdvsql.cn/article22/ceoscc.html
成都網站建設公司_創新互聯,為您提供服務器托管、網站維護、網站設計、網站改版、移動網站建設、標簽優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯