Does the Array_rand () function have a random number of elements of an array that are randomly obtained from another array?

Source: Internet
Author: User
The Array_rand () function says so in the document:

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

Parameters
Input
The input array.
Num_req
Indicates how many units you want to take out of the unit. If the specified number exceeds the number in the array, a e_warning level error is generated.

return value
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. Extracts one or more random cells from an array and returns one or more keys for a random entry.

So, to get the key name of the group will be repeated, the internet for a long time seems to have no explanation for this

Reply content:

The Array_rand () function says so in the document:

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

Parameters
Input
The input array.
Num_req
Indicates how many units you want to take out of the unit. If the specified number exceeds the number in the array, a e_warning level error is generated.

return value
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. Extracts one or more random cells from an array and returns one or more keys for a random entry.

So, to get the key name of the group will be repeated, the internet for a long time seems to have no explanation for this

There is absolutely no repetition, I paste the main part of the source code, the algorithm is to loop through the array of elements, and then by the algorithm comparison, matching the key value into the return value, until the number of matching key is obtained or the array element cycle.

.

php//键名应该不会有重复$arr = [1,2,3];$res = array_rand($arr,3);//总是[0,1,2]echo $arr[$res[0]];//1echo $arr[$res[0]];//2echo $arr[$res[0]];//3

Random elements suggest replacing with shufflearray_rand
http://www.php.net/manual/en/function.array-rand.php

Key names are definitely not duplicated.

The array's key name 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.