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

Posted 1O(∩_∩)O1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二维数组排序 按某个字段排序相关的知识,希望对你有一定的参考价值。

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

 

以上是关于二维数组排序 按某个字段排序的主要内容,如果未能解决你的问题,请参考以下文章

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

php 根据某个字段对二维数组这个数组排序

PHP:根据二维数组中的某个字段进行排序

php 二维数组 根据某个字段排序

thinkPHP二维数组排序

php针对二维数组的某个字段进行排序