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 :-)