php從數組中隨機抽取一些元素代碼
來源:程序員人生 發布時間:2013-10-26 07:06:29 閱讀次數:2818次
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];
- }
- }
-
- return $getArray;
- }
- }
-
- $keywords = array(
- "我們",
- "你們",
- "他們"
- );
- $getValue=new getValues();
- $getValue->inputValue($keywords);
- $key = $getValue->getValue(1);
- ?>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈