Version 3.3 of the PHP Map package includes more methods to work with collections of strings easily:
- strContains(): Tests if the passed string is part of one of the entries
- strStarts(): Tests if one of the entries starts with the passed string
- strEnds(): Tests if one of the entries ends with the passed string
- strLower(): Converts all alphabetic characters to lower case
- strUpper(): Converts all alphabetic characters to upper case
All these methods are multi-byte character aware and use UTF-8 encoding by default.
Examples:
“` Map::from( [‘abc’] )->strContains( ‘b’ ); // true
Map::from( [‘abc’] )->strStarts( ‘ab’ ); // true
Map::from( [‘abc’] )->strEnds( ‘bc’ ); // true
Map::from( [‘ΤΆΧΙΣΤΗ’] )->strUpper(); // [“τάχιστη”]
Map::from( [‘τάχιστη’] )->strUpper(); // [“ΤΆΧΙΣΤΗ”] “`
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 🙂
submitted by /u/aimeos
[link] [comments]
More Stories
Will County, Illinois 1864 Map – May 20, 2023 at 04:14AM
This kid on Google Map trying to get by – April 27, 2023 at 05:05PM
World of Hyatt: Complete list of all-inclusive properties in Europe (with map) – April 27, 2023 at 04:57PM