yarri/array-sort

PHP 中排序数组的 definitive 函数

v0.1 2024-01-23 13:44 UTC

This package is auto-updated.

Last update: 2024-09-06 12:29:51 UTC


README

Build Status

PHP 中排序数组的 definitive 函数。

函数签名

array_sort(array $array): array
array_sort(array $array, array $options = []): array
array_sort(array $array, int $flags = SORT_LOCALE_STRING): array
array_sort(array $array, int $flags = SORT_LOCALE_STRING, array $options = []): array
array_sort(array $array, int $flags = SORT_LOCALE_STRING, callable $callback = null, array $options = []): array
array_sort(array $array, callback $callback = null, array $options = []): array

参数

array

输入数组。

flags

参数 flags 可以使用这些值来修改排序行为

  • SORT_REGULAR - 正常比较项;详细信息在比较运算符部分描述
  • SORT_NUMERIC - 数字比较项
  • SORT_STRING - 将项作为字符串比较
  • SORT_LOCALE_STRING - 根据当前区域设置将项作为字符串比较。它使用区域设置,可以通过 setlocale() 来更改
  • SORT_NATURAL - 使用 "自然排序"(如 natsort())将项作为字符串比较
  • SORT_FLAG_CASE - 可以与 SORT_STRING 或 SORT_NATURAL 结合(按位或)以对字符串进行不区分大小写的排序

callback

参数 callback 是一个函数,用于将数组项转换为它的字符串或数字形式。如果它设置为 NULL,则使用默认函数

$callback = function($item){ return (string)$item; };

options

默认选项是

Array(
  "sort_keys" => false,
  "reverse" => false,
  "preserve_keys" => false,
)

返回值

array_sort 函数返回排序后的数组。

用法

$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];

array_sort($fruits); // ["apple", "banana", "lemon", "orange"]
array_sort($fruits,["reverse" => true]); // ["orange", "lemon", "banana", "apple"]

array_sort($fruits,["preserve_keys" => true]); // ["c" => "apple", "b" => "banana", "d" => "lemon", "a" => "orange"]
array_sort($fruits,["preserve_keys" => true, "reverse" => true]); // ["a" => "orange", "d" => "lemon", "b" => "banana", "c" => "apple"]

// === SORTING KEYS ===

array_sort($fruits,["sort_keys" => true]); // ["a" => "orange", "b" => "banana", "c" => "apple", "d" => "lemon"]
array_sort($fruits,["sort_keys" => true, "reverse" => true]); // ["d" => "lemon", "c" => "apple", "b" => "banana", "a" => "orange"]

// === NUMERIC SORTING ===

$numbers = ["d" => 200, "a" => 100, "b" => 20, "c" => 10];

// not yet :)
array_sort($numbers); // [10, 100, 20, 200];

array_sort($numbers,SORT_NUMERIC); // [10, 20, 100, 200]
array_sort($numbers,SORT_NUMERIC,["reverse" => true]); // [200, 100, 20, 10]

array_sort($numbers,SORT_NUMERIC,["preserve_keys" => true]); // ["c" => 10, "b" => 20, "a" => 100, "d" => 200]

// === SORTING OF STRUCTURES ===

$books = [
  [
    "author" => "Verne, Jules",
    "title" => "Around the World in Eighty Days",
  ],
  [
    "author" => "Tolstoy, Leo",
    "title" => "War and Peace",
  ],
  [
    "author" => "King, Stephen",
    "title" => "The X-Files",
  ],
  [
    "author" => "Toole, John Kennedy",
    "title" => "A Confederacy of Dunces",
  ],
];

// sorting by the author
array_sort($books, function($book){ return $book["author"]; });
// [
//   [
//     "author" => "King, Stephen",
//     "title" => "The X-Files",
//   ],
//   [
//     "author" => "Tolstoy, Leo",
//     "title" => "War and Peace",
//   ],
//   [
//     "author" => "Toole, John Kennedy",
//     "title" => "A Confederacy of Dunces",
//   ],
//   [
//     "author" => "Verne, Jules",
//     "title" => "Around the World in Eighty Days",
//   ],
// ]

// sorting by the title without article
array_sort($books, function($book){ return preg_replace("/^(a|the) /i","",$book["title"]); });
// [
//   [
//     "author" => "Verne, Jules",
//     "title" => "Around the World in Eighty Days",
//   ],
//   [
//     "author" => "Toole, John Kennedy",
//     "title" => "A Confederacy of Dunces",
//   ],
//   [
//     "author" => "Tolstoy, Leo",
//     "title" => "War and Peace",
//   ],
//   [
//     "author" => "King, Stephen",
//     "title" => "The X-Files",
//   ],
// ]

安装

只需使用 Composer

composer require yarri/array-sort

许可

ArraySort 是免费软件,根据 MIT 许可证的条款分发 https://open-source.org.cn/licenses/mit-license