3 ms·
While that is correct, I would definetely prefer using the syntax used in Symfony ArrayCollection[1]: $collection = new ArrayCollection([4,3,2,1]); $collec
by sandreas 5y ago
While that is correct, I would definetely prefer using the syntax used in Symfony ArrayCollection[1]:
$collection = new ArrayCollection([4,3,2,1]);
$collection->filter(fn($element) => $element % 2 == 0)
->map(fn($element) => $element+2)
->toArray();
It is pretty much like in JavaScript or C# with Linq, but with less features.
But you can implement this yourself using
- ArrayInterface, Traversable, Countable, etc.
- generators / iterators (yield)
It's not that hard. There is also YaLinqo[2]
[1] https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html https://www.doctrine-project.org/projects/doctrine-collectio...
[2] https://github.com/Athari/YaLinqo https://github.com/Athari/YaLinqo
- Minor49er 5y agoVery useful for more complex situations. Thanks for this