elephox / collection
Elephox collection library.
Installs: 201
Dependents: 12
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
pkg:composer/elephox/collection
Requires
- php: ^8.1 <8.3
- jetbrains/phpstorm-attributes: ^1.0
- dev-develop
- 0.9.0.x-dev
- 0.8.0.x-dev
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.3
- 0.4.0
- 0.4.0-alpha5
- 0.4.0-alpha4
- 0.4.0-alpha3
- 0.4.0-alpha2
- 0.4.0-alpha1
- 0.3.27
- v0.3.25
- v0.3.24
- v0.3.23
- v0.3.22
- v0.3.21
- v0.3.20
- v0.3.18
- v0.3.17
- v0.3.16
- v0.3.15
- v0.3.14
- v0.3.13
- v0.3.12
- v0.3.11
- v0.3.10
- v0.3.9
- v0.3.8
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.1
- dev-features/db
- dev-release/0.9
- dev-release/0.8
- dev-release/0.7
- dev-16-psr-20-clock
- dev-18-make-servicecollection-more-extensible
- dev-release/0.6
- dev-release/0.5
- dev-release/0.4
- dev-release/0.3
This package is auto-updated.
Last update: 2025-10-13 05:49:35 UTC
README
This library (or rather module) was inspired by C#s LINQ library.
However, it is not a full-featured LINQ library. It is only a small subset of the functionality since user-land PHP code cannot fully support all the syntactic sugar. For example, the main feature of LINQ, SQL-like syntax directly in source, is not supported since it would require you to compile/transpile your code, which is not in the scope of this library.
The main idea, however, is to provide a way to iterate over a collection of objects in a more natural/handy way like you can do with IEnumerables in C#.
Examples
<?php declare(strict_types=1); use Elephox\Collection\Enumerable; $data = [5, 2, 1, 4, 3]; $arr = Enumerable::from($data); $arr->orderBy(fn (int $item) => $item) ->select(function (int $item) { echo $item; }); // output: 12345 $arr->where(fn (int $item) => $item % 2 === 0) ->select(function (int $item) { echo $item; }); // output: 24 echo $arr->aggregate(fn (int $acc, int $item) => $acc + $item, 0); // output: 15
Differences to C# IEnumerable
- PHP doesn't support generics. For now, only static analyzers like Psalm can provide full type safety when working with generic collections.
- No extension methods from the System.Datanamespace are implemented (CopyToDataTable)
- Castwouldn't make sense in a dynamically typed language, so it is not implemented either. You can use- Enumerable::selectto change the type of the values.
- GroupJoinis not implemented (yet?)
- Methods ending with OrDefaultalways hasnullas the default value. You can, of course, pass your own default value.
- PHP has no default comparer for types, so we provide a DefaultEqualityComparerclass that implements some methods to compare two values. Depending on if an order is required,DefaultEqualityComparer::sameorDefaultEqualityComparer::compareis used if you don't provide a comparer function yourself.
- LongCountis not implemented since PHP only has one integer type
- OfTypeis not implemented since PHP doesn't have generics
- ToHashSet,- ToDictionaryand- ToLookupare not implemented. Instead, you can convert an- Enumerableto native types via- toListand- toArray(whereas- toArraykeeps the keys or allows you to pass a key selector function)
- AsParalleland- AsQueryableare not implemented
- None of the System.Xmlextension methods are implemented (Ancestors,Descendants,Elements, etc.)
- No read only or immutable interfaces or methods to get them exist (yet?)
- TryGetNonEnumeratedCountis not implemented