coercive / iterator
Coercive Utility Iterator
0.1.0
2026-10-20 13:00 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-20 13:05:52 UTC
README
Item iterator
Compatible PHP 7.4 → 8.5
Get
composer require coercive/iterator
Tests
composer install
vendor/bin/phpunit
PHPUnit 12 requires PHP 8.3+ to run the tests ; the library itself still runs on PHP 7.4.
Example Multibyte String Iterator
foreach (new MbStrIterator('string') as $i => $chr) { echo "$i => $chr \n"; }
The key is the position in bytes of the character ('éa' gives 0 => 'é', 2 => 'a').
Example Array Sort
$examples = [ 1 => ['id' => 'abcd'], 2 => ['id' => 'ABCD'], 3 => ['id' => 'aabcd'], 4 => ['id' => 'aAbcd'], 5 => ['id' => 'AAbcd'], ]; # Instantiate with data $array = new Sort($examples); # [optional] Set case-insensitive if useful $array->insensitive(true); # [optional] Set targeted key level (0 : any level, default) $array->level(1); # Sort ASCENDING $sorted = $array->asc('id'); # Sort DESCENDING $sorted = $array->desc('id'); # The level can also be given directly $sorted = $array->asc('id', 1);
The keys of the source array are kept.
The key can be found deeper in a row : when a row holds it several times, its lowest value is used for asc(), its highest for desc().
The rows without the key are kept at the end, in their original order.