array_multisort这个函数还是很有用的。

---------------------------------------------------------------------------------

array_multisort

(PHP 4, PHP 5, PHP 7)

array_multisort — Sort multiple or multi-dimensional arrays

Description

bool array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed$array1_sort_flags = SORT_REGULAR [, mixed $... ]]] )

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.

Associative (string) keys will be maintained, but numeric keys will be re-indexed.

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

Parameters

array1

An array being sorted.

array1_sort_order

The order used to sort the previous array argument. Either SORT_ASC to sort ascendingly or SORT_DESC to sort descendingly.

This argument can be swapped with array1_sort_flags or omitted entirely, in which case SORT_ASC is assumed.

array1_sort_flags

Sort options for the previous array argument:

Sorting type flags:

  • SORT_REGULAR - compare items normally (don't change types)
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

This argument can be swapped with array1_sort_order or omitted entirely, in which case SORT_REGULARis assumed.

...

More arrays, optionally followed by sort order and flags. Only elements corresponding to equivalent elements in previous arrays are compared. In other words, the sort is lexicographical.

Return Values

Returns TRUE on success or FALSE on failure.

Changelog

Version Description
5.4.0 The SORT_NATURAL and SORT_FLAG_CASE were added to array1_sort_flags as possible sort flags.
5.3.0 The SORT_LOCALE_STRING was added to array1_sort_flags as possible sort flags.

Examples

Example #1 Sorting multiple arrays

<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

  1. array(4) {
  2. [0]=> int(0)
  3. [1]=> int(10)
  4. [2]=> int(100)
  5. [3]=> int(100)
  6. }
  7. array(4) {
  8. [0]=> int(4)
  9. [1]=> int(1)
  10. [2]=> int(2)
  11. [3]=> int(3)
  12. }

Example #2 Sorting multi-dimensional array

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order).

  1. array(2) {
  2. [0]=> array(5) {
  3. [0]=> string(2) "10"
  4. [1]=> int(100)
  5. [2]=> int(100)
  6. [3]=> int(11)
  7. [4]=> string(1) "a"
  8. }
  9. [1]=> array(5) {
  10. [0]=> int(1)
  11. [1]=> int(3)
  12. [2]=> string(1) "2"
  13. [3]=> int(2)
  14. [4]=> int(1)
  15. }
  16. }

Example #3 Sorting database results

For this example, each element in the data array represents one row in a table. This type of dataset is typical of database records.

Example data:

  1. volume | edition
  2. -------+--------
  3. 67 | 2
  4. 86 | 1
  5. 85 | 6
  6. 98 | 2
  7. 86 | 6
  8. 67 | 7

The data as an array, called data. This would usually, for example, be obtained by looping with mysqli_fetch_assoc().

<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>

In this example, we will order by volume descending, edition ascending.

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// as of PHP 5.5.0 you can use array_column() instead of the above code
$volume  = array_column($data, 'volume');
$edition = array_column($data, 'edition');

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

The dataset is now sorted, and will look like this:

  1. volume | edition
  2. -------+--------
  3. 98 | 2
  4. 86 | 1
  5. 86 | 6
  6. 85 | 6
  7. 67 | 2
  8. 67 | 7

php 中array_multisort排序,类似于对数据库中的记录依次按多列进行排序的更多相关文章

  1. Java实现购物车功能:方式一:存放在session中.方式二:存储在数据库中

    //将购物车产品加入到cookie中,方式同浏览记录.Java实现购物车,方式一(简易版):存储在session中.这种方式实现还不严谨,大家看的时候看思路即可.(1). JSP页面中,选择某一款产品 ...

  2. ArcCatalog中通过ArcSDE向Oracle数据库中导入数据

    将数据导入到Oracle指定的表空间的具体内容如下: 首先,在ArcCatalog中建立指定表空间的数据库连接(要以指定表空间的用户登录): 然后,在ArcCatlog中定位到数据源,选中并拷贝图层; ...

  3. SqlBulkCopy将DataTable中的数据批量插入数据库中

    #region 使用SqlBulkCopy将DataTable中的数据批量插入数据库中 /// <summary> /// 注意:DataTable中的列需要与数据库表中的列完全一致.// ...

  4. 多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中【我】

    多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中 package com.xxx.xx.reve.service; import java.util.ArrayL ...

  5. jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上

    jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上 1.JavaBean的使用 package com.zheng; public class BookBean { ...

  6. 使用Sqoop,最终导入到hive中的数据和原数据库中数据不一致解决办法

            Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL , ...

  7. Java中线程的锁和数据库中的事务隔离级别

    当涉及到两个或多个线程操作同一个资源时,就会出现锁的问题. 数据库中的某一条记录或者是某一个对象中的字段,可以修改,也可以读取,一般情况下,读取的那个方法应该加锁(即用synchronized互斥), ...

  8. 定时任务中的备份不同的数据库中的所有的表,每个表使用单独的sql备份文件

    #! /bin/bash # 指定用户 USER=root # 指定密码 PASS=123456 # 指定主机地址 HOST=localhost # 指定备份的目录 BACKUP=/backup/sq ...

  9. logstash将redis中的队列中数据发送到influxdb数据库中

    通过elk获取到的java jvm中ygc的时间如下: 现在讲ygc字段的值,发送到influxdb中 首先安装logstash的插件 logstash-output-influxdb 安装完成后,查 ...

随机推荐

  1. enumerate()和map()函数用法

    一.python enumerate用法 先出一个题目: 1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出: 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出 ...

  2. 洛谷P2234 [HNOI2002] 营业额统计 [splay]

    题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  3. Python类总结-继承-子类和父类,新式类和经典类

    子类和父类 class Father(object): #子类在使用super调用父类时,Father后面要加object --新式类 def __init__(self): self.Fname = ...

  4. 【BZOJ 2298】 2298: [HAOI2011]problem a (DP)

    2298: [HAOI2011]problem a Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1326  Solved: 637 Descript ...

  5. [NC13A]反蝴蝶效应/[SPOJ-NPC2014D]General Joke

    [NC13A]反蝴蝶效应/[SPOJ-NPC2014D]General Joke 题目大意: 按顺序访问\(A_{1\sim n}(n\le10^5)\),经过\(A_i\)时身上必须有\(A_i\) ...

  6. SpringBoot返回结果如果为null或空值不显示处理方法

    第一种方法:自定义消息转换器 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter{ // /** // * ...

  7. Codechef SEP14 QRECT cdq分治+线段树

    题意 支持删除矩阵.插入矩阵.查询当前矩阵与之前有多少个矩阵相交 算相交的时候容斥一下:相交矩形数 = 总矩形数-X轴投影不相交的矩形数-Y轴投影不相交的矩形数-XY轴投影下都不相交的矩形数 最后一项 ...

  8. [转]Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件

      Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件. 1.为了防止拖动ListView时,在列表末尾重复数据显示.需要加入 HashMap<In ...

  9. 在Windows下将Redis注册为本地服务

    当前redis版本:3.2.100 通常情况下我们可以通过 redis-server.exe 和配置文件启动redis服务 : redis-server.exe redis.windows.conf ...

  10. jquery datatables使用

    引入相应css 和js <link href="http://cdn.datatables.net/1.10.5/css/jquery.dataTables.css" rel ...