php 数组元素随机排序代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-05 16:37:30 浏览: 评论:0
因为工作需要我要对我定义好的数组进行随机排序,每一次循环都要是不同的,下面我们利用了php shuffle函数随机对数组元素进行排序,方法非常简单.
shuffle() 函数把数组中的元素按随机顺序重新排列,若成功,则返回 true,否则返回 false.
注释:本函数为数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序,代码如下:
- */
- $array = array(1,2,3,4);
- for( $i=1;$i<30;$i++)
- {
- shuffle( $array );
- print_r($array);
- echo '<br />';//开源代码phpfensi.com
- }
- /*
- array ( [0] => 3 [1] => 2 [2] => 4 [3] => 1 )
- array ( [0] => 3 [1] => 4 [2] => 1 [3] => 2 )
- array ( [0] => 2 [1] => 1 [2] => 3 [3] => 4 )
- array ( [0] => 2 [1] => 4 [2] => 1 [3] => 3 )
- array ( [0] => 3 [1] => 4 [2] => 2 [3] => 1 )
- array ( [0] => 3 [1] => 2 [2] => 4 [3] => 1 )
- array ( [0] => 3 [1] => 4 [2] => 1 [3] => 2 )
- array ( [0] => 4 [1] => 2 [2] => 1 [3] => 3 )
- array ( [0] => 2 [1] => 4 [2] => 1 [3] => 3 )
- array ( [0] => 4 [1] => 3 [2] => 1 [3] => 2 )
- array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
- array ( [0] => 4 [1] => 3 [2] => 1 [3] => 2 )
- array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 )
- array ( [0] => 2 [1] => 1 [2] => 4 [3] => 3 )
- array ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 )
- array ( [0] => 2 [1] => 1 [2] => 3 [3] => 4 )
- array ( [0] => 4 [1] => 1 [2] => 3 [3] => 2 )
- array ( [0] => 1 [1] => 4 [2] => 2 [3] => 3 )
- array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
- array ( [0] => 1 [1] => 2 [2] => 4 [3] => 3 )
- array ( [0] => 1 [1] => 3 [2] => 4 [3] => 2 )
- array ( [0] => 2 [1] => 4 [2] => 3 [3] => 1 )
- array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
- array ( [0] => 1 [1] => 3 [2] => 4 [3] => 2 )
- array ( [0] => 2 [1] => 1 [2] => 4 [3] => 3 )
- array ( [0] => 4 [1] => 1 [2] => 3 [3] => 2 )
- array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
- array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
- array ( [0] => 2 [1] => 1 [2] => 4 [3] => 3 )
总结:php有大量的函数,如果你知道这个函数会对你开发提高大量的帮助,如果你不知道要自己写可能花很多时间.
Tags: php 数组元素 随机排序
- 上一篇:php多维数组排序与操作函数
- 下一篇:php unset删除数组指定元素方法代码
相关文章
- ·php in_array 语法(2013-11-12)
- ·PHP中数组定义的几种方法(2013-11-13)
- ·php session 预定义数组(2013-11-13)
- ·php 读取多维数组方法(2013-11-28)
- ·php 数组之删除空数组程序(2013-11-30)
- ·php删除数组中的空值或指定值操作(2013-11-30)
- ·php对数组元素去重复值(2013-12-03)
- ·php中向数组中插入一元素程序代码(2014-01-03)
- ·php中数组的搜索程序代码(2014-01-06)
- ·php二维数组循环程序代码(2014-01-06)
- ·php数组删除元素各种方法总结(2014-01-06)
- ·php 数组排序各种方法总结(2014-01-06)
- ·PHP数组的高级遍历和操作处理方法(2014-01-07)
- ·PHP获取数组中某元素的位置实例(2014-01-07)
- ·PHP取数组第一个元素与删除数组实例(2014-01-07)
- ·php将数组变量保存到文件(2014-01-08)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)