I have an array called $ran = array(1,2,3,4);
我有一个名为$ran = array的数组(1,2,3,4);
I need to get a random value out of this array and store it in a variable, how can I do this?
我需要从这个数组中得到一个随机值并将它存储在一个变量中,我怎么做呢?
12 个解决方案
#1
170
You can also do just:
你也可以这样做:
$k = array_rand($array);
$v = $array[$k];
This is the way to do it when you have an associative array.
这是当你有一个关联数组时的方法。
#2
22
PHP provides a function just for that: array_rand()
http://php.net/manual/en/function.array-rand.php
PHP为此提供了一个函数:array_rand() http://php.net/manual/en/function.array-rand.php
$ran = array(1,2,3,4);
$randomElement = $ran[array_rand($ran, 1)];
#3
18
You can use mt_rand()
您可以使用mt_rand()
$random = $ran[mt_rand(0, count($ran) - 1)];
This comes in handy as a function as well if you need the value
如果需要这个值,这个函数也很有用
function random_value($array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
return isset($array[$k])? $array[$k]: $default;
}
#4
12
$value = $array[array_rand($array)];
#5
8
You could use the array_rand function to select a random key from your array like below.
可以使用array_rand函数从数组中选择一个随机键,如下所示。
$array = array("one", "two", "three", "four", "five", "six");
echo $array[array_rand($array, 1)];
or you could use the rand and count functions to select a random index.
或者可以使用rand和count函数来选择一个随机索引。
$array = array("one", "two", "three", "four", "five", "six");
echo $array[rand(0, count($array) - 1)];
#6
6
The array_rand
function seems to have an uneven distribution on large arrays, not every array item is equally likely to get picked. Using shuffle on the array and then taking the first element doesn't have this problem:
array_rand函数在大数组上的分布似乎不均匀,并不是每个数组项都一样容易被选中。在数组中使用shuffle,然后取第一个元素,没有这个问题:
$myArray = array(1, 2, 3, 4, 5);
// Random shuffle
shuffle($myArray);
// First element is random now
$randomValue = $myArray[0];
#7
5
Derived from Laravel Collection::random()
:
来自Laravel集合:随机():
function array_random($array, $amount = 1)
{
$keys = array_rand($array, $amount);
if ($amount == 1) {
return $array[$keys];
}
$results = [];
foreach ($keys as $key) {
$results[] = $array[$key];
}
return $results;
}
Usage:
用法:
$items = ['foo', 'bar', 'baz', 'lorem'=>'ipsum'];
array_random($items); // 'bar'
array_random($items, 2); // ['foo', 'ipsum']
A few notes:
一些笔记:
-
$amount
has to be less than or equal tocount($array)
. - $金额必须小于或等于count($array)。
-
array_rand()
doesn't shuffle keys (since PHP 5.2.10, see 48224), so your picked items will always be in original order. Useshuffle()
afterwards if needed. - array_rand()不洗牌键(因为PHP 5.2.10,请参见48224),所以您所选的项目将始终保持原样。如果需要,在之后使用shuffle()。
Documentation: array_rand()
, shuffle()
文档:用于(),洗牌()
edit: The Laravel function has noticeably grown since then, see Laravel 5.4's Arr::random()
. Here is something more elaborate, derived from the grown-up Laravel function:
编辑:Laravel函数从那时起显著增长,参见Laravel 5.4的Arr::random()。这里有一些更精细的东西,源自于成人拉拉维尔函数:
function array_random($array, $number = null)
{
$requested = ($number === null) ? 1 : $number;
$count = count($array);
if ($requested > $count) {
throw new \RangeException(
"You requested {$requested} items, but there are only {$count} items available."
);
}
if ($number === null) {
return $array[array_rand($array)];
}
if ((int) $number === 0) {
return [];
}
$keys = (array) array_rand($array, $number);
$results = [];
foreach ($keys as $key) {
$results[] = $array[$key];
}
return $results;
}
A few highlights:
几个亮点:
#8
3
$rand = rand(1,4);
or, for arrays specifically:
或者,特别对数组:
$array = array('a value', 'another value', 'just some value', 'not some value');
$rand = $array[ rand(0, count($array)-1) ];
#9
2
Does your selection have any security implications? If so, use random_int()
and array_keys()
. (random_bytes()
is PHP 7 only, but there is a polyfill for PHP 5).
您的选择是否有任何安全影响?如果是,使用random_int()和array_keys()。(random_bytes()仅为PHP 7,但PHP 5有一个polyfill)。
function random_index(array $source)
{
$max = count($source) - 1;
$r = random_int(0, $max);
$k = array_keys($source);
return $k[$r];
}
Usage:
用法:
$array = [
'apple' => 1234,
'boy' => 2345,
'cat' => 3456,
'dog' => 4567,
'echo' => 5678,
'fortune' => 6789
];
$i = random_index($array);
var_dump([$i, $array[$i]]);
Demo: https://3v4l.org/1joB1
演示:https://3v4l.org/1joB1
#10
2
Use rand() to get random number to echo random key. In ex: 0 - 3
使用rand()获取随机数以响应随机密钥。例:0 - 3
$ran = array(1,2,3,4);
echo $ran[rand(0,3)];
#11
0
I'm basing my answer off of @ÓlafurWaage's function. I tried to use it but was running into reference issues when I had tried to modify the return object. I updated his function to pass and return by reference. The new function is:
我的答案基于@OlafurWaage的函数。我试图使用它,但在尝试修改return对象时遇到了引用问题。我更新了他的功能,以供参考。新功能:
function &random_value(&$array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
if (isset($array[$k])) {
return $array[$k];
} else {
return $default;
}
}
For more context, see my question over at Passing/Returning references to object + changing object is not working
对于更大的上下文,请参见我的问题,在传递/返回对对象+更改对象的引用时不工作。
#12
-5
You get a random number out of an array as follows:
你从数组中得到一个随机数如下:
$randomValue = array_rand($rand,1);
#1
170
You can also do just:
你也可以这样做:
$k = array_rand($array);
$v = $array[$k];
This is the way to do it when you have an associative array.
这是当你有一个关联数组时的方法。
#2
22
PHP provides a function just for that: array_rand()
http://php.net/manual/en/function.array-rand.php
PHP为此提供了一个函数:array_rand() http://php.net/manual/en/function.array-rand.php
$ran = array(1,2,3,4);
$randomElement = $ran[array_rand($ran, 1)];
#3
18
You can use mt_rand()
您可以使用mt_rand()
$random = $ran[mt_rand(0, count($ran) - 1)];
This comes in handy as a function as well if you need the value
如果需要这个值,这个函数也很有用
function random_value($array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
return isset($array[$k])? $array[$k]: $default;
}
#4
12
$value = $array[array_rand($array)];
#5
8
You could use the array_rand function to select a random key from your array like below.
可以使用array_rand函数从数组中选择一个随机键,如下所示。
$array = array("one", "two", "three", "four", "five", "six");
echo $array[array_rand($array, 1)];
or you could use the rand and count functions to select a random index.
或者可以使用rand和count函数来选择一个随机索引。
$array = array("one", "two", "three", "four", "five", "six");
echo $array[rand(0, count($array) - 1)];
#6
6
The array_rand
function seems to have an uneven distribution on large arrays, not every array item is equally likely to get picked. Using shuffle on the array and then taking the first element doesn't have this problem:
array_rand函数在大数组上的分布似乎不均匀,并不是每个数组项都一样容易被选中。在数组中使用shuffle,然后取第一个元素,没有这个问题:
$myArray = array(1, 2, 3, 4, 5);
// Random shuffle
shuffle($myArray);
// First element is random now
$randomValue = $myArray[0];
#7
5
Derived from Laravel Collection::random()
:
来自Laravel集合:随机():
function array_random($array, $amount = 1)
{
$keys = array_rand($array, $amount);
if ($amount == 1) {
return $array[$keys];
}
$results = [];
foreach ($keys as $key) {
$results[] = $array[$key];
}
return $results;
}
Usage:
用法:
$items = ['foo', 'bar', 'baz', 'lorem'=>'ipsum'];
array_random($items); // 'bar'
array_random($items, 2); // ['foo', 'ipsum']
A few notes:
一些笔记:
-
$amount
has to be less than or equal tocount($array)
. - $金额必须小于或等于count($array)。
-
array_rand()
doesn't shuffle keys (since PHP 5.2.10, see 48224), so your picked items will always be in original order. Useshuffle()
afterwards if needed. - array_rand()不洗牌键(因为PHP 5.2.10,请参见48224),所以您所选的项目将始终保持原样。如果需要,在之后使用shuffle()。
Documentation: array_rand()
, shuffle()
文档:用于(),洗牌()
edit: The Laravel function has noticeably grown since then, see Laravel 5.4's Arr::random()
. Here is something more elaborate, derived from the grown-up Laravel function:
编辑:Laravel函数从那时起显著增长,参见Laravel 5.4的Arr::random()。这里有一些更精细的东西,源自于成人拉拉维尔函数:
function array_random($array, $number = null)
{
$requested = ($number === null) ? 1 : $number;
$count = count($array);
if ($requested > $count) {
throw new \RangeException(
"You requested {$requested} items, but there are only {$count} items available."
);
}
if ($number === null) {
return $array[array_rand($array)];
}
if ((int) $number === 0) {
return [];
}
$keys = (array) array_rand($array, $number);
$results = [];
foreach ($keys as $key) {
$results[] = $array[$key];
}
return $results;
}
A few highlights:
几个亮点:
#8
3
$rand = rand(1,4);
or, for arrays specifically:
或者,特别对数组:
$array = array('a value', 'another value', 'just some value', 'not some value');
$rand = $array[ rand(0, count($array)-1) ];
#9
2
Does your selection have any security implications? If so, use random_int()
and array_keys()
. (random_bytes()
is PHP 7 only, but there is a polyfill for PHP 5).
您的选择是否有任何安全影响?如果是,使用random_int()和array_keys()。(random_bytes()仅为PHP 7,但PHP 5有一个polyfill)。
function random_index(array $source)
{
$max = count($source) - 1;
$r = random_int(0, $max);
$k = array_keys($source);
return $k[$r];
}
Usage:
用法:
$array = [
'apple' => 1234,
'boy' => 2345,
'cat' => 3456,
'dog' => 4567,
'echo' => 5678,
'fortune' => 6789
];
$i = random_index($array);
var_dump([$i, $array[$i]]);
Demo: https://3v4l.org/1joB1
演示:https://3v4l.org/1joB1
#10
2
Use rand() to get random number to echo random key. In ex: 0 - 3
使用rand()获取随机数以响应随机密钥。例:0 - 3
$ran = array(1,2,3,4);
echo $ran[rand(0,3)];
#11
0
I'm basing my answer off of @ÓlafurWaage's function. I tried to use it but was running into reference issues when I had tried to modify the return object. I updated his function to pass and return by reference. The new function is:
我的答案基于@OlafurWaage的函数。我试图使用它,但在尝试修改return对象时遇到了引用问题。我更新了他的功能,以供参考。新功能:
function &random_value(&$array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
if (isset($array[$k])) {
return $array[$k];
} else {
return $default;
}
}
For more context, see my question over at Passing/Returning references to object + changing object is not working
对于更大的上下文,请参见我的问题,在传递/返回对对象+更改对象的引用时不工作。
#12
-5
You get a random number out of an array as follows:
你从数组中得到一个随机数如下:
$randomValue = array_rand($rand,1);