码迷,mamicode.com
首页 > 编程语言 > 详细

二维数组排序 按某个字段排序

时间:2018-11-24 14:39:52      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:key   技术分享   volume   value   tps   pre   tail   sort   inf   

来源:https://blog.csdn.net/m0_38027358/article/details/79494357

核心:array_multisort()函数

定义规则,升降序,排列数组

 1 <?php
 2 $arr = array(
 3     array(
 4         id => 5,
 5         time  => 2018-01-05
 6     ),
 7     array(
 8         id => 3,
 9         time  => 2018-01-03
10     ),
11     array(
12         id => 1,
13         time  => 2018-01-01
14     ),
15     array(
16         id => 2,
17         time  => 2018-01-02
18     ),
19     array(
20         id => 4,
21         time  => 2018-01-04
22     )
23 );
24 $arrSort = array();
25 foreach($arr AS $key => $value){
26     foreach($value AS $k=>$v){
27         $arrSort[$k][$key] = $v;
28     }
29 }
30 /**
31 $arrSort=Array
32 (
33     [id] => Array
34         (
35             [0] => 5
36             [1] => 3
37             [2] => 1
38             [3] => 2
39             [4] => 4
40         )
41     [time] => Array
42         (
43             [0] => 2018-01-05
44             [1] => 2018-01-03
45             [2] => 2018-01-01
46             [3] => 2018-01-02
47             [4] => 2018-01-04
48         )
49 )
50 */
51  
52 array_multisort($arrSort[time], SORT_ASC, $arr);
53 print_r($arr);

技术分享图片

 

 

 1 <?php
 2 $data[] = array(volume => 67, edition => 2);
 3 $data[] = array(volume => 86, edition => 1);
 4 $data[] = array(volume => 85, edition => 6);
 5 $data[] = array(volume => 98, edition => 2);
 6 $data[] = array(volume => 86, edition => 6);
 7 $data[] = array(volume => 67, edition => 7);
 8 foreach ($data as $key => $row) {
 9     $volume[$key]  = $row[volume];
10 }
11 array_multisort($volume, SORT_DESC, $data);
12 print_r($data);
13 ?>

技术分享图片

 

二维数组排序 按某个字段排序

标签:key   技术分享   volume   value   tps   pre   tail   sort   inf   

原文地址:https://www.cnblogs.com/laijinquan/p/10011294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!