博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 中array_multisort排序,类似于对数据库中的记录依次按多列进行排序
阅读量:4630 次
发布时间:2019-06-09

本文共 4889 字,大约阅读时间需要 16 分钟。

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

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

array_multisort

(PHP 4, PHP 5, PHP 7)

array_multisort — Sort multiple or multi-dimensional arrays

Description

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

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

Associative () 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  being sorted.

array1_sort_order

The order used to sort the previous  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  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 
  • SORT_NATURAL - compare items as strings using "natural ordering" like 
  • 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.

array(4) {  [0]=> int(0)  [1]=> int(10)  [2]=> int(100)  [3]=> int(100)}array(4) {  [0]=> int(4)  [1]=> int(1)  [2]=> int(2)  [3]=> int(3)}

 

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).

array(2) {  [0]=> array(5) {    [0]=> string(2) "10"    [1]=> int(100)    [2]=> int(100)    [3]=> int(11)    [4]=> string(1) "a"  }  [1]=> array(5) {    [0]=> int(1)    [1]=> int(3)    [2]=> string(1) "2"    [3]=> int(2)    [4]=> int(1)  }}

 

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:

volume | edition-------+--------    67 |       2    86 |       1    85 |       6    98 |       2    86 |       6    67 |       7

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

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

volume | edition-------+--------    98 |       2    86 |       1    86 |       6    85 |       6    67 |       2    67 |       7

转载于:https://www.cnblogs.com/oxspirt/p/9559012.html

你可能感兴趣的文章
数组溢界地址的正确使用: 即 int a[6] 中的 a[-1] 和 a[6] 正确使用
查看>>
怎样退出App之前唤醒还有一个App?
查看>>
-bash:jps:command not found
查看>>
cogs 998. [東方S2] 帕秋莉·诺蕾姬
查看>>
BZOJ 1019: [SHOI2008]汉诺塔
查看>>
jquery ocupload一键上传文件应用
查看>>
Java并发编程-看懂AQS的前世今生
查看>>
洛谷 [P3480] KAM-Pebbles
查看>>
操作系统任务调度问题
查看>>
day02-python 基础02
查看>>
.net下Ueditor配置(主要讲解上传功能配置)
查看>>
std::string的Copy-on-Write:不如想象中美好
查看>>
KONG -- 配置 service 并添加 key-auth
查看>>
多重继承和有内嵌对象时构造函数调用顺序
查看>>
C#编码规范
查看>>
信号、槽位及布局
查看>>
webpack + vue
查看>>
启动JvisualVM提示"无法检测到本地java应用程序"的解决方案
查看>>
抓包实现脚本编写--手持机
查看>>
PHP+nginx 线上服务研究(一)
查看>>