PHP uses the array array_rand () function to efficiently and randomly extract the records of the specified number of bars, which can be randomly extracted from the database, suitable for random display and lottery programs.
The algorithm mainly uses PHP's Array_rand () function, the following look at the main functions of the Array_rand () function:
array_rand-random extraction of one or more cells from an array
Mixed Array_rand (array $input [, int $num _req])
Array_rand () is useful when you want to remove one or more random cells from an array. It accepts input as an input array and an optional parameter, Num_req, indicating how many units you want to remove-if not specified, the default is 1.
If you only take one, Array_rand () returns the key name of a random cell, or returns an array containing the random key name. This allows you to randomly remove the key names and values from the array.
Let's look at a small example:
Copy the Code code as follows:
Srand (float) microtime () * 10000000);
$input = Array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand _keys = Array_rand ($input, 2);
Print $input [$rand _keys[0]]. "\ n";
Print $input [$rand _keys[1]]. "\ n";
?>
The function randomly extracts the record number in the $list array, requiring that the contents of the record need only the output.
Here is an example of a database that randomly extracts specific records:
Copy the Code code as follows:
$list = $DB->where ("Status=1 and Flag=1")->order (' sort ')->select ();
Unset ($Case);
$count = count ($list);
$rand _list=range (0, $count-1);
$rand _list = Array_rand ($rand _list, 10);
$tuijian _array = Array ();
foreach ($rand _list as $key) {
$tuijian _array[] = $list [$key];
}
unset ($case _list);
Print_r ($tuijian _array);
?>
http://www.bkjia.com/PHPjc/313637.html www.bkjia.com true http://www.bkjia.com/PHPjc/313637.html techarticle PHP uses the array array_rand () function to efficiently and randomly extract the records of the specified number of bars, which can be randomly extracted from the database, suitable for random display and lottery programs. The algorithm is mainly ...