arifulhb/php-algorithms
Composer 安装命令:
composer require arifulhb/php-algorithms
包简介
Multi-dimensional Array & Object Sorting in PHP
README 文档
README
Sort a multidimensional array or object by String or integer or both types.
Installation
Install the package on your php application.
composer require arifulhb/php-algorithms
Sort by Array
For example, if you have an array, and you want to sort by name or by price or both, you can use this package to sort.
$items = [
[
'name' => 'computer',
'price' => 4,
],
[
'name' => 'tv',
'price' => 9,
],
[
'name' => 'apple',
'price' => 1,
],
[
'name' => 'airpod',
'price' => 1,
],
[
'name' => 'airtag',
'price' => 3,
],
[
'name' => 'shampoo',
'price' => 5,
]
];
How to
// create a configuration object specifying integer and string field accordingly.
$config = new SortConfiguration('price', 'name');
// create sort object with the configuration
$sortArray = new SortArray($config);
// sort the array
$result = $sortArray->sort($products);
Sort by Object
Same as sort by array, but here you may have an array of object to sort. For example,
$arrayOfObjects = [
new Product('computer', 4),
new Product('tv', 9),
new Product('apple', 1),
new Product('airpod', 1),
new Product('airtag', 3),
new Product('shampoo', 5)
];
How to
// create a configuration object specifying integer and string field accordingly.
$config = new SortConfiguration('price', 'name');
// create sort object with the configuration
$sortObject = new SortObject($config);
// sort the array
$result = $sortObject->sort($products);
Development
To run the test cases,
composer run-script tests
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-08