Home  >  Article  >  Backend Development  >  PHP code to randomly extract some elements from an array_PHP tutorial

PHP code to randomly extract some elements from an array_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:58830browse

Copy code The code is as follows:

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 ); // Sort from small to large, modify as needed
return $getArray;
}
}

//Test code
$ keywords = array(
"we",
"you",
"them"
);
$getValue=new getValues();
$getValue->inputValue( $keywords);
$key = $getValue->getValue(1); // Randomly extract an element from the array
echo $key;
? >

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326147.htmlTechArticleCopy the code as follows: ?php class getValues ​​{ public function inputValue($inputArray) { $this-inputArray = $ inputArray; } public function getValue($number) { $this-number = $nu...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn