百科狗-知识改变命运!
--

shuffle() - 打乱数组 - php 数组函数

乐乐1年前 (2023-11-21)阅读数 22#技术干货
文章标签数组

shuffle()

(PHP 4, PHP 5, PHP 7)

打乱数组

说明

shuffle(array &$array): bool

本函数打乱(随机排列单元的顺序)一个数组。它使用的是伪随机数产生器,并不适合密码学的场合。

Note:

shuffle() - 打乱数组 - php 数组函数

If two members compare as equal, their relative order in the sorted array is undefined.

参数

$array

待操作的数组。

返回值

成功时返回TRUE,或者在失败时返回FALSE

更新日志

版本说明
7.1.0内置的随机数产生算法从 libc rand 函数改成»梅森旋转伪随机数生成算法。

范例

Example #1shuffle()例子

注释

Note:此函数为$array中的元素赋与新的键名。这将删除原有的键名,而不是仅仅将键名重新排序。

参见

  • array_rand() 从数组中随机取出一个或多个单元
  • 数组排序函数对比
shuffle for associative arrays, preserves key=>value pairs.
(Based on (Vladimir Kornea of typetango.com)'s function) 

*note: as of PHP 5.2.10, array_rand's resulting array of keys is no longer shuffled, so we use array_keys + shuffle.
Shuffle associative and non-associative array while preserving key, value pairs. Also returns the shuffled array instead of shuffling it in place. 
This seems to do reasonably well as a shuffle() that preserves index assocation:

Obviously only works if PHP has closures enabled...
I needed a simple function two shuffle a two dimensional array. Please note the second level arrays must be indexed using integers, for example $myarray[0]["Name"] and not $myarray["One"]["Name"]. Here is the function:

Please note it only works on two dimensional arrays. Here is an example:

Hope you find it useful!
If you want the Power Set (set of all unique subsets) of an array instead of permutations, you can use this simple algorithm: 
There is an function which uses native shuffle() but preserves keys, and their order, so at end, only values are shuffled.

Result example:
Before (original):
Array
(
  [ZERO] => 0
  [ONE] => 1
  [TWO] => 2
  [THREE] => 3
  [FOUR] => 4
  [FIVE] => 5
  [SIX] => 6
  [SEVEN] => 7
  [EIGHT] => 8
  [NINE] => 9
)
After (Array Quake);
Array
(
  [ZERO] => 4
  [ONE] => 2
  [TWO] => 0
  [THREE] => 8
  [FOUR] => 3
  [FIVE] => 6
  [SIX] => 1
  [SEVEN] => 7
  [EIGHT] => 5
  [NINE] => 9
)
Building on examples by m227 and pineappleclock, here is a function that returns all permutations of each set in the power set of an array of strings (instead of a string). Thanks for the great examples! 
Here i wrote a custom shuffle function which preserves the array index and distributes the array element randomly. 
Here is a quick function I wrote that generates a random password and uses shuffle() to easily shuffle the order. 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)