PHP randomly extracts elements from an array

From , 4 Years ago, written in PHP, viewed 52 times.
URL https://pastebin.vip/view/38ed162a
  1. <? php
  2. class getValues {
  3.     public function inputValue($inputArray) {
  4.         $this->inputArray = $inputArray;
  5.     }
  6.     public function getValue($number) {
  7.         $this->number = $number;
  8.         for($i = 0; $i < $this->number; $i ++) {
  9.             $index = rand ( 0, count ( $this->inputArray ) - 1 - $i );
  10.             $getArray [$i] = $this->inputArray [$index];
  11.             unset ( $this->inputArray [$index] );
  12.             for($k = $index; $k < count ( $this->inputArray ) - 1; $k ++) {
  13.                 $this->inputArray [$k] = $this->inputArray [$k + 1];
  14.             }
  15.         }
  16.         //asort ( $getArray ); // 从小到大排序,根据需要修改
  17.         return $getArray;
  18.     }
  19. }
  20.  
  21. //测试代码
  22. $keywords = array(
  23.                 "我们",
  24.                 "你们",
  25.                 "他们"
  26.             );
  27. $getValue = new getValues();
  28. $getValue->inputValue($keywords);
  29. $key = $getValue->getValue(1);//从数组中随机抽取一个元素
  30. echo $key;
  31. >
  32. //该片段来自于http://yuncode.net
  33.  

Reply to "PHP randomly extracts elements from an array"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website