sebastiansulinski / sorter
排序多维数组和对像。
v1.0.0
2016-01-28 17:44 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ^5.1
This package is auto-updated.
Last update: 2024-09-05 19:50:12 UTC
README
本包允许您对多维数组和对象进行排序。
安装
通过composer安装包
composer require sebastiansulinski/sorter
使用说明
1. 简单数组
$array = [
[
'name' => 'Arthur'
],
[
'name' => 'Tom'
],
[
'name' => 'Mark'
]
];
$sorter = new Sorter($array);
// sort the array by 'name' key
$sorter->asc('name');
// two ways of printing array
echo $sorter->printR();
echo $sorter;
// get resulting, previously sorted array
$array_sorted_asc = $sorter->collection();
// reverse order and return the resulting array
$array_sorted_desc = $sorter->desc('name')->collection();
以上结果的排序
// $array_sorted_asc
Array
(
[0] => Array
(
[name] => Arthur
)
[1] => Array
(
[name] => Mark
)
[2] => Array
(
[name] => Tom
)
)
// $array_sorted_desc
Array
(
[0] => Array
(
[name] => Tom
)
[1] => Array
(
[name] => Mark
)
[2] => Array
(
[name] => Arthur
)
)
2. 多维数组
$array = [
[
'person' => [
'name' => 'Arthur'
]
],
[
'person' => [
'name' => 'Tom'
]
],
[
'person' => [
'name' => 'Mark'
]
]
];
$sorter = new Sorter($array);
// sort ascending by child 'name' key
$array_sorted_asc = $sorter->asc(['person', 'name'])->collection();
// sort descending by child 'name' key
$array_sorted_desc = $sorter->desc(['person', 'name'])->collection();
结果
// $array_sorted_asc
Array
(
[0] => Array
(
[person] => Array
(
[name] => Arthur
)
)
[1] => Array
(
[person] => Array
(
[name] => Mark
)
)
[2] => Array
(
[person] => Array
(
[name] => Tom
)
)
)
// $array_sorted_desc
Array
(
[0] => Array
(
[person] => Array
(
[name] => Tom
)
)
[1] => Array
(
[person] => Array
(
[name] => Mark
)
)
[2] => Array
(
[person] => Array
(
[name] => Arthur
)
)
)
3. 对象
为了本练习的目的,假设我们有两个属性和3个类的对像
- name : 字符串
- wallet : 钱包
以及一个 Wallet
类,它有一个属性
- amount : 整数
首先,让我们尝试根据对像的 name
属性对3个对像实例进行排序
$collection[] = new Tutor(['name' => 'John']);
$collection[] = new Tutor(['name' => 'Craig']);
$collection[] = new Student(['name' => 'Marcus']);
$collection[] = new Student(['name' => 'George']);
$collection[] = new Student(['name' => 'Albert']);
$collection[] = new Student(['name' => 'Tony']);
$collection[] = new Staff(['name' => 'Greg']);
$collection[] = new Staff(['name' => 'Antony']);
$sorter = new Sorter($collection);
// sort ascending by child 'name' property
$array_sorted_asc = $sorter->asc('name')->collection();
// sort descending by child 'name' property
$array_sorted_desc = $sorter->desc('name')->collection();
$array_sorted_asc
的排序结果
Array
(
[0] => SSD\Student Object
(
[name] => Albert
[wallet] =>
)
[1] => SSD\Staff Object
(
[name] => Antony
[wallet] =>
)
[2] => SSD\Tutor Object
(
[name] => Craig
[wallet] =>
)
[3] => SSD\Student Object
(
[name] => George
[wallet] =>
)
[4] => SSD\Staff Object
(
[name] => Greg
[wallet] =>
)
[5] => SSD\Tutor Object
(
[name] => John
[wallet] =>
)
[6] => SSD\Student Object
(
[name] => Marcus
[wallet] =>
)
[7] => SSD\Student Object
(
[name] => Tony
[wallet] =>
)
)
$array_sorted_desc
的排序结果
Array
(
[0] => SSD\Student Object
(
[name] => Tony
[wallet] =>
)
[1] => SSD\Student Object
(
[name] => Marcus
[wallet] =>
)
[2] => SSD\Tutor Object
(
[name] => John
[wallet] =>
)
[3] => SSD\Staff Object
(
[name] => Greg
[wallet] =>
)
[4] => SSD\Student Object
(
[name] => George
[wallet] =>
)
[5] => SSD\Tutor Object
(
[name] => Craig
[wallet] =>
)
[6] => SSD\Staff Object
(
[name] => Antony
[wallet] =>
)
[7] => SSD\Student Object
(
[name] => Albert
[wallet] =>
)
)
4. 对象属性的属性
Sorter 允许您根据内部/嵌套对像的属性值来排序对像。
我们的3个类有一个名为 wallet
的属性,它是 Wallet
对象类型。让我们尝试使用 Wallet
对象的 amount
属性来排序我们的3个对像。
$collection[] = new Tutor([
'name' => 'John',
'wallet' => new Wallet(['amount' => 10])
]);
$collection[] = new Tutor([
'name' => 'Craig',
'wallet' => new Wallet(['amount' => 12])
]);
$collection[] = new Student([
'name' => 'Marcus',
'wallet' => new Wallet(['amount' => 13])
]);
$collection[] = new Student([
'name' => 'George',
'wallet' => new Wallet(['amount' => 21])
]);
$collection[] = new Student([
'name' => 'Albert',
'wallet' => new Wallet(['amount' => 36])
]);
$collection[] = new Student([
'name' => 'Tony',
'wallet' => new Wallet(['amount' => 50])
]);
$collection[] = new Staff([
'name' => 'Greg',
'wallet' => new Wallet(['amount' => 22])
]);
$collection[] = new Staff([
'name' => 'Antony',
'wallet' => new Wallet(['amount' => 31])
]);
$sorter = new Sorter($collection);
// sort ascending by 'amount' property of the embedded object
$array_sorted_asc = $sorter->asc(['wallet', 'amount'])->collection();
// sort descending by 'amount' property of the embedded object
$array_sorted_desc = $sorter->desc(['wallet', 'amount'])->collection();
$array_sorted_asc
的排序结果
Array
(
[0] => SSD\Tutor Object
(
[name] => John
[wallet] => SSD\Wallet Object
(
[amount] => 10
)
)
[1] => SSD\Tutor Object
(
[name] => Craig
[wallet] => SSD\Wallet Object
(
[amount] => 12
)
)
[2] => SSD\Student Object
(
[name] => Marcus
[wallet] => SSD\Wallet Object
(
[amount] => 13
)
)
[3] => SSD\Student Object
(
[name] => George
[wallet] => SSD\Wallet Object
(
[amount] => 21
)
)
[4] => SSD\Staff Object
(
[name] => Greg
[wallet] => SSD\Wallet Object
(
[amount] => 22
)
)
[5] => SSD\Staff Object
(
[name] => Antony
[wallet] => SSD\Wallet Object
(
[amount] => 31
)
)
[6] => SSD\Student Object
(
[name] => Albert
[wallet] => SSD\Wallet Object
(
[amount] => 36
)
)
[7] => SSD\Student Object
(
[name] => Tony
[wallet] => SSD\Wallet Object
(
[amount] => 50
)
)
)
$array_sorted_desc
的排序结果
Array
(
[0] => SSD\Student Object
(
[name] => Tony
[wallet] => SSD\Wallet Object
(
[amount] => 50
)
)
[1] => SSD\Student Object
(
[name] => Albert
[wallet] => SSD\Wallet Object
(
[amount] => 36
)
)
[2] => SSD\Staff Object
(
[name] => Antony
[wallet] => SSD\Wallet Object
(
[amount] => 31
)
)
[3] => SSD\Staff Object
(
[name] => Greg
[wallet] => SSD\Wallet Object
(
[amount] => 22
)
)
[4] => SSD\Student Object
(
[name] => George
[wallet] => SSD\Wallet Object
(
[amount] => 21
)
)
[5] => SSD\Student Object
(
[name] => Marcus
[wallet] => SSD\Wallet Object
(
[amount] => 13
)
)
[6] => SSD\Tutor Object
(
[name] => Craig
[wallet] => SSD\Wallet Object
(
[amount] => 12
)
)
[7] => SSD\Tutor Object
(
[name] => John
[wallet] => SSD\Wallet Object
(
[amount] => 10
)
)
)