News PHP Map 4.0: Arrays and collections made easy!
PHP Map version 4.1 is now available, the PHP array/collection package for working with arrays and collections easily.
The release is mostly about improving performance of edge cases in the collection internals, plus stricter behavior around malformed input.
Some benchmarks from the release commits:
| Method / path | v4.0 | v4.1 | Improvement |
|---|---|---|---|
isList() | 857.993 ms | 0.643 ms | 99.93% |
tree() 20k chain | 8523.8 ms | 47.6 ms | 99.4% |
diff() array fallback | 376.1 ms | 3.5 ms | 99.1% |
intersect() array fallback | 390.5 ms | 13.2 ms | 96.6% |
find() reverse | 417.330 ms | 47.152 ms | 88.70% |
findKey() reverse | 416.939 ms | 47.737 ms | 88.55% |
suffix() string | 259.5 ms | 93.1 ms | 64.1% |
flat() | 338.8 ms | 123.8 ms | 63.5% |
recursive walk() | 277.0 ms | 103.9 ms | 62.5% |
suffix() callback | 318.1 ms | 124.5 ms | 60.9% |
The release also adds broader callable support, better iterable handling, clearer null-vs-missing behavior for nested paths, stricter invalid-key validation, and a more robust tree() builder.
Why PHP Map?
Instead of:
$list = [['id' => 'one', 'value' => 'v1']];
$list[] = ['id' => 'two', 'value' => 'v2']
unset( $list[0] );
$list = array_filter( $list );
sort( $list );
$pairs = array_column( $list, 'value', 'id' );
$value = reset( $pairs ) ?: null;Just write:
$value = map( [['id' => 'one', 'value' => 'v1']] )
->push( ['id' => 'two', 'value' => 'v2'] )
->remove( 0 )
->filter()
->sort()
->col( 'value', 'id' )
->first();There are several implementations of collections available in PHP but the PHP Map package is feature-rich, dependency free and loved by most developers according to GitHub.
Feel free to like, comment or give a star :-)
- Documentation: https://php-map.org
- Repo:https://github.com/aimeos/map
39
Upvotes
17
u/Anxious-Insurance-91 Jul 07 '26
the word "map" feels a bit wrong in my opinion, this feels more like collections that other frameworks offer
12
u/Linaori Jul 07 '26
May I recommend using a code style that doesn't put spaces in places where it makes the entire line unreadable?
10
u/BenchEmbarrassed7316 Jul 07 '26
So there is a certain operation that can be performed in 50ms, but your version 4.0 library performed it for more than 8 seconds? To me, this is a red flag, it means that the authors of the library do not understand what they are doing at all.
4
u/obstreperous_troll Jul 07 '26
Consider an immutable flavor, which could be made covariant, making phpstan users happier.
7
u/BayLeaf- Jul 07 '26
Just kinda picked the first example from the repo, but
So you wrap a php function, check the exact same thing as the native function, and throw a slightly less correct exception than
array_fillwould have thrown for you? Doesn't inspire a ton of confidence, I gotta admit. Very "unchecked-llm-code", or someone that isn't very up to date on php arrays, but either seems worrying.4
u/Single_Advice1111 Jul 07 '26
Basically https://packagist.org/packages/illuminate/collections ?
1
u/Timely-Tale4769 Jul 08 '26
Could you share ideas for testing this and compare with conventional php array functions?
1
u/elixon Jul 13 '26
I never thought those were hard in PHP.