fbpx
April 19, 2025

PHP Map 3.4 released – Collections made easy! – January 19, 2023 at 06:33AM

Version 3.4 of the PHP Map package includes more methods to work with collections of strings easily:

  • strContainsAll(): Test if all entries contains a string
  • strStartsAll(): Test if all entries starts with string
  • strEndsAll(): Test if all entries ends with string
  • strReplace(): Replace chars in all strings
  • trim(): Trim chars from all strings
  • cast(): Cast entries to the passed type if possible

All these methods are multi-byte character aware and use UTF-8 encoding by default. The strContainsAll(), strStartsAll() and strEndsAll() methods were suggested by /u/ckdot

Examples:

“`php Map::from( [‘abc’, ‘bca’] )->strContainsAll( ‘bc’ ); // true

Map::from( [‘abc’, ‘acb’] )->strStartsAll( ‘a’ ); // true

Map::from( [‘abc’, ‘def’] )->strEndsAll( [‘c’, ‘f’] ); // true

Map::from( [‘google.com’, ‘aimeos.com’] )->strReplace( ‘.com’, ‘.org’ ); // [‘google.org’, ‘aimeos.org’]

Map::from( [“a b c”, “cbax”] )->trim( ‘abc’ ); // [” b “, “x”]

Map::from( [true, 1, 1.0, ‘yes’] )->cast( ‘int’ ); // [1, 1, 1, 0] “`

For more examples, look at https://php-map.org

Why PHP Map?

Instead of:

php $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:

php $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 🙂

https://php-map.org

submitted by /u/aimeos
[link] [comments]

%d bloggers like this: