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

来源: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 ?>

 

posted @ 2018-11-24 10:47  1O(∩_∩)O1  阅读(308)  评论(0编辑  收藏  举报