Download the PHP package sgh/comparable-arrays without Composer
On this page you can find all versions of the php package sgh/comparable-arrays. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sgh/comparable-arrays
More information about sgh/comparable-arrays
Files in sgh/comparable-arrays
Package comparable-arrays
Short Description Provides generic Comparators for arrays.
License MIT
Homepage https://github.com/schmengler/comparable-arrays
Informations about the package comparable-arrays
Comparable-Arrays
This package provides Comparators for arrays and objects that implement the SPL ArrayAccess
interface.
They can be used with the sorting and comparing tools in the Comparable package.
Requirements
The package requires PHP 5.4 or later and the Comparable package in version 1.0 or later.
Install
Via Composer
Usage
The following comparators are available:
KeyComparator
: Use array item with specific key for comparisonMultiKeyComparator
: The same, but with the option to sort by multiple keys (i.e. fall back in case of equality)
You can use all the methods in \SGH\Comparable\SortFunctions
and \SGH\Comparable\SetFunctions
with the comparators.
KeyComparator Example
The default comparator used to compare the array items is NumericComparator
, which compares any scalar values and treats
them as numbers. To sort by title we needed to specify the comparator explicitly.
If you prefer, you can also use the factory method ::callback()
to retrieve a comparison callback, that can be used
in any function that expects a user defined comparison callback:
usort($arrayOfBooks, KeyComparator::callback('title', new StringComparator));
MultiKeyComparator Example
Configuration
Both, KeyComparator
and MultiKeyComparator
accept arrays and objects that implement ArrayAccess
as parameters.
This default behavior can be changed such that they only accept arrays:
By default array keys must exist to compare their values. Both comparators have a non-strict mode, where missing items are treated as smaller than everything else. Two missing items are treated as equal.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Fabian Schmengler(https://github.com/schmengler)
- All Contributors
License
The MIT License (MIT). Please see License File for more information.