PHP uses the array array_rand() function to efficiently randomly extract a specified number of records. It can randomly extract records in the database and is suitable for random display and lottery programs.
This algorithm mainly uses the array_rand() function of PHP. Let’s take a look at the main functions of the array_rand() function:
array_rand—randomly remove one or more cells from the 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, which specifies how many cells you want to remove - if not specified, it defaults to 1.
If you only take out one, array_rand() returns the key name of a random unit, otherwise it returns an array containing random key names. This way you can randomly pull out keys and values from the array.
Let’s take a look at a small example:
This function will randomly select the record number in the $list array. If you need to record the content, you only need to output it.
The following is an example of randomly selecting specific records from the database: