PHP array_rand是否返回唯一键?

时间:2021-06-25 21:33:46

If I call array_rand($array, 100) does it avoid selecting the same key multiple times?

如果我调用array_rand($array, 100),它会避免多次选择相同的键吗?

3 个解决方案

#1


5  

Yes, within one call the return set should be unique.

是的,在一个调用中,返回集应该是唯一的。

#2


1  

Yes it does! Just read the documentation: http://php.net/array_rand

是的它!只需阅读文档:http://php.net/array_rand。

Or better: try by yourself to get it giving you twice the same key.

或者更好:自己试着让它给你两倍的钥匙。

 $ php -a
 php > print_r(array_rand(array(1,2), 2));
 Array
 (
      [0] => 0
      [1] => 1
 )

#3


0  

As array_rand throws a warning when specifying a number larger than the array-size I assume keys will be unique.

当array_rand在指定大于数组大小的数字时抛出警告时,我假设键是唯一的。

#1


5  

Yes, within one call the return set should be unique.

是的,在一个调用中,返回集应该是唯一的。

#2


1  

Yes it does! Just read the documentation: http://php.net/array_rand

是的它!只需阅读文档:http://php.net/array_rand。

Or better: try by yourself to get it giving you twice the same key.

或者更好:自己试着让它给你两倍的钥匙。

 $ php -a
 php > print_r(array_rand(array(1,2), 2));
 Array
 (
      [0] => 0
      [1] => 1
 )

#3


0  

As array_rand throws a warning when specifying a number larger than the array-size I assume keys will be unique.

当array_rand在指定大于数组大小的数字时抛出警告时,我假设键是唯一的。