An algorithm for efficiently and randomly extracting specified bar records by array in PHP _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.