php 二维数组排序使用详解
发布:smiling 来源: PHP粉丝网 添加日期:2018-05-31 10:18:20 浏览: 评论:0
昨天要排序数组的时候发现了,要按时间排序,但是php并没有内设这个函数,所以在网上找到了这个代码,第一个参数为数组,第二个是要排序的元素,第三个为排序方式,下面就是php 二维数组排序的代码:
- function arraySort($arr, $keys, $type = 'asc') {
- $keysvalue = $new_array = array();
- foreach ($arr as $k => $v){
- $keysvalue[$k] = $v[$keys];
- }
- $type == 'asc' ? asort($keysvalue) : arsort($keysvalue);
- reset($keysvalue);
- foreach ($keysvalue as $k => $v) {
- $new_array[$k] = $arr[$k];
- }
- return $new_array;
- }
- $arr[] = array("name"=>"1","time"=>1) ;
array_multisort(array1,sorting order, sorting type,array2,array3..)是对多个数组或多维数组进行排序的函数。
- <?php
- function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
- if(is_array($arrays)){
- foreach ($arrays as $array){
- if(is_array($array)){
- $key_arrays[] = $array[$sort_key];
- }else{
- return false;
- }
- }
- }else{
- return false;
- }
- array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
- return $arrays;
- }
- $person = array(
- array('id'=>1,'name'=>'fj','weight'=>100,'height'=>180),
- array('id'=>2,'name'=>'tom','weight'=>53,'height'=>150),
- array('id'=>3,'name'=>'jerry','weight'=>120,'height'=>156),
- array('id'=>4,'name'=>'bill','weight'=>110,'height'=>190),
- array('id'=>5,'name'=>'linken','weight'=>80,'height'=>200),
- array('id'=>6,'name'=>'madana','weight'=>95,'height'=>110),
- array('id'=>7,'name'=>'jordan','weight'=>70,'height'=>170)
- );
- var_dump($person);
- $person = my_sort($person,'name',SORT_ASC,SORT_STRING);
- var_dump($person);
- $person = my_sort($person,'weight');
- var_dump($person);
- ?>
结果如下:
- array (size=7)
- 0 =>
- array (size=4)
- 'id' => int 1
- 'name' => string 'fj' (length=2)
- 'weight' => int 100
- 'height' => int 180
- 1 =>
- array (size=4)
- 'id' => int 2
- 'name' => string 'tom' (length=3)
- 'weight' => int 53
- 'height' => int 150
- 2 =>
- array (size=4)
- 'id' => int 3
- 'name' => string 'jerry' (length=5)
- 'weight' => int 120
- 'height' => int 156
- 3 =>
- array (size=4)
- 'id' => int 4
- 'name' => string 'bill' (length=4)
- 'weight' => int 110
- 'height' => int 190
- 4 =>
- array (size=4)
- 'id' => int 5
- 'name' => string 'linken' (length=6)
- 'weight' => int 80
- 'height' => int 200
- 5 =>
- array (size=4)
- 'id' => int 6
- 'name' => string 'madana' (length=6)
- 'weight' => int 95
- 'height' => int 110
- 6 =>
- array (size=4)
- 'id' => int 7
- 'name' => string 'jordan' (length=6)
- 'weight' => int 70
- 'height' => int 170
- array (size=7)
- 0 =>
- array (size=4)
- 'id' => int 4
- 'name' => string 'bill' (length=4)
- 'weight' => int 110
- 'height' => int 190
- 1 =>
- array (size=4)
- 'id' => int 1
- 'name' => string 'fj' (length=2)
- 'weight' => int 100
- 'height' => int 180
- 2 =>
- array (size=4)
- 'id' => int 3
- 'name' => string 'jerry' (length=5)
- 'weight' => int 120
- 'height' => int 156
- 3 =>
- array (size=4)
- 'id' => int 7
- 'name' => string 'jordan' (length=6)
- 'weight' => int 70
- 'height' => int 170
- 4 =>
- array (size=4)
- 'id' => int 5
- 'name' => string 'linken' (length=6)
- 'weight' => int 80
- 'height' => int 200
- 5 =>
- array (size=4)
- 'id' => int 6
- 'name' => string 'madana' (length=6)
- 'weight' => int 95
- 'height' => int 110
- 6 =>
- array (size=4)
- 'id' => int 2
- 'name' => string 'tom' (length=3)
- 'weight' => int 53
- 'height' => int 150
- array (size=7)
- 0 =>
- array (size=4)
- 'id' => int 2
- 'name' => string 'tom' (length=3)
- 'weight' => int 53
- 'height' => int 150
- 1 =>
- array (size=4)
- 'id' => int 7
- 'name' => string 'jordan' (length=6)
- 'weight' => int 70
- 'height' => int 170
- 2 =>
- array (size=4)
- 'id' => int 5
- 'name' => string 'linken' (length=6)
- 'weight' => int 80
- 'height' => int 200
- 3 =>
- array (size=4)
- 'id' => int 6
- 'name' => string 'madana' (length=6)
- 'weight' => int 95
- 'height' => int 110
- 4 =>
- array (size=4)
- 'id' => int 1
- 'name' => string 'fj' (length=2)
- 'weight' => int 100
- 'height' => int 180
- 5 =>
- array (size=4)
- 'id' => int 4
- 'name' => string 'bill' (length=4)
- 'weight' => int 110
- 'height' => int 190
- 6 =>
- array (size=4)
- 'id' => int 3
- 'name' => string 'jerry' (length=5)
- 'weight' => int 120
- 'height' => int 156
这里的重点就是,先把要排序的key存到一个一维数组中,然后就可以使用array_multisort()这个函数,将数组按照key进行排序了,当然,这里的排序你完全可以不适用array_multisort()这个函数,仅仅通过foreach遍历也能达到这个效果,但是既然php开发者给我们提供了更好的办法,我们就可以省去不必要的麻烦了。
Tags: 二维 数组
- 上一篇:php中有序的数组打印或排序的例子
- 下一篇:php 判断是否有重复数组
相关文章
- ·删除数组与二维数组值的php代码(2013-11-29)
- ·php二维数组循环程序代码(2014-01-06)
- ·php 二维数组按指定的键值排序(2014-01-17)
- ·php一维二维数组键排序方法总结(2014-01-17)
- ·php 二维数组以某一键名进行分组相加(2014-02-17)
- ·php二维数组的合并2种方法(2014-02-17)
- ·php给数组赋值(二维数组赋值)详解(2014-02-23)
- ·php输出二维数组(2014-05-10)
- ·php遍历二维数组 foreach函数(2014-06-04)
- ·php 定义二维数组与数组定义方法(2014-06-04)
- ·PHP二维数组怎么按某个字段排序?(2014-07-01)
- ·PHP二维数组去重复项自定义函数(2014-07-08)
- ·php二维数组排序后获取最大值(2014-07-14)
- ·PHP二维数组排序问题详解(数组键值排序)(2014-07-15)
- ·php 二维数组的分组排序实现代码(2014-07-21)
- ·php去除二维数组中重复值程序(2014-07-28)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)