Does the array_rand () function randomly retrieve a certain number of array elements from another array?

Source: Internet
Author: User
The array_rand () function is described in the document as follows: {code...} parameter input array. Num_req indicates the number of units you want to fetch. If the specified number exceeds the number in the array, an E_WARNING error is generated. If you retrieve only one... array_rand () function in this document:

mixed array_rand ( array $input [, int $num_req = 1 ] )

Parameters
Input
Input array.
Num_req
Specify the number of units you want to fetch. If the specified number exceeds the number in the array, an E_WARNING error is generated.

Return Value
If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array. Extract one or more random units from the array and return one or more keys of the random entries.

So, will the key names of the obtained array be repeated? It seems that this is not explained for a long time on the Internet.

Reply content:

The array_rand () function is described in the document as follows:

mixed array_rand ( array $input [, int $num_req = 1 ] )

Parameters
Input
Input array.
Num_req
Specify the number of units you want to fetch. If the specified number exceeds the number in the array, an E_WARNING error is generated.

Return Value
If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array. Extract one or more random units from the array and return one or more keys of the random entries.

So, will the key names of the obtained array be repeated? It seems that this is not explained for a long time on the Internet.

There will never be duplicates. I will paste the main part of the source code. The algorithm loops through the elements in the array in sequence and compares them with algorithms one by one. when the conditions are met, the key value is placed in the return value, until the number of keys that meet the conditions is obtained or the array element is cyclically completed.

.

php// The key name should not be repeated $ arr = [, 3]; $ res = array_rand ($ arr, 3); // always, 2] echo $ arr [$ res [0]; // 1 echo $ arr [$ res [0]; // 2 echo $ arr [$ res [0]; // 3

Shuffle is recommended for random elements.array_rand
Http://www.php.net/manual/en/function.array-rand.php

The key names are certainly not repeated.

The key names of the array will never be repeated.

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.