PHP: Array > Map 2.0

The new 2.0 major release of the PHP map package is available which includes new methods and support for working with multi-dimensional arrays in an easy way. Additionally, it comes with the best documentation ever so you find everything you need quickly!

Instead of applying independent PHP functions on arrays, use the PHP Map object to stream array data through several methods to transform, order and shorten its elements until the result exactly conforms to your requirements, for example:

$data = [
    ['name' => 'Tom', 'dep' => 'Dev', 'qty' => 30],
    ['name' => 'Bob', 'dep' => 'Sales', 'qty' => 50],
    ['name' => 'Joe', 'dep' => 'Dev', 'qty' => 10]
];
 
// get the person with the highest quantity in "Dev" department
$name = map( $data )
    ->groupBy( 'dep' )->only( 'Dev' )
    ->flat( 1 )->col( 'name', 'qty' )
    ->ksort()->last();

To get the result in one statement is easier and much more elegant than the traditional way in PHP. The PHP Map object is perfectly suited to work on result sets returned by relational databases. You can transform them until you get a collection that exactly fits for your template generating the HTML for the frontend.

The PHP Map package combines the best from Laravel and CakePHP collections, jQuery, Backbone.js, and Lodash in an easy to use PHP package for every PHP application.

What’s new in PHP Map 2.0

One of the biggest new feature is the ability to pass a path of keys as parameter to several PHP Map methods to work on nested arrays directly. Instead of writing own code for diving into multi-dimensional arrays, you can now use the available methods to e.g. get a value by using:

map( ['key' => ['to' => ['value' => 'yes!']]] )
    ->get( 'key/to/value' ); // returns "yes!"

This also works for more advanced methods like col() to reindex an array by using a nested key:

$data = [
    ['name' => 'Tom', 'addr' => ['zip' => '12AB', 'city' => 'NY']],
    ['name' => 'Bob', 'addr' => ['zip' => '23WD', 'city' => 'LA']]
];
 
$map = map( $data )->col( 'name', 'addr/zip' );
// ['12AB' => 'Tom', '23WD' => 'Bob']

By default, a dot (“/”) is used as seperator between the array key names but you can change that for the current map using the sep() method:

$map = map( $data )->sep( '.' )->col( 'name', 'addr.zip' );

If you want to use another seperator in all new PHP Map objects (existing ones will keep their separator), you can change the delimiter globally with the delimiter() method:

Map::delimiter( ',' );

It also returns the delimiter used before if you want to revert the used delimiter later in your code.

Breaking changes in PHP Map 2.0

The new major version contains a few breaking changes to avoid potential problems that came up in the past. You won’t be affected by most of them but you should have a look to prevent unexpected behaviour.

New slash separator

The most important change affects parameter values including slashes because they are now treated as path separators for nested arrays.

$data = [
    'item/id' => 1,
    'item/code' => 'test',
    'item/name' => 'Test'
];
 
map( $data )->col( 'item/code', 'item/id' );
 
// 1.x
[1 => 'test']
 
// 2.x
[]
// use instead
map( $data )->sep( '.' )->col( 'item/code', 'item/id' );

jQuery style method calls

You can call methods of objects in a map like this:

// MyClass implements setStatus() (returning $this) and
// getCode() (initialized by constructor)
 
$map = Map::from( [
    'a' => new MyClass( 'x' ),
    'b' => new MyClass( 'y' )
] );
$map->setStatus( 1 )->getCode()->toArray();

Before, the objects had been checked if they really implement the setStatus() and getCode() methods.

To avoid returning an empty map if the method name is wrong or the called method is catched by the __call() magic method, this isn’t the case any more. If the method isn’t implemented by all objects, PHP generates a fatal error now for better error tracing.

Second equals() parameter

The $assoc parameter of the equals() method to compare keys too has been removed and now, you must use the is() method instead:

// 1.x
map( ['one' => 1] )->equals( ['one' => 1], true );
 
// 2.x
map( ['one' => 1] )->is( ['one' => 1] );

New find() argument

The find() method now accepts a default value or exception object as second argument and the $reverse argument is at the third position now:

// 1.x
Map::from( ['a', 'c', 'e'] )->find( function( $value, $key ) {
    return $value >= 'b';
}, true );
 
// 2.x
Map::from( ['a', 'c', 'e'] )->find( function( $value, $key ) {
    return $value >= 'b';
}, null, true );

Semantic change in groupBy()

Before, items have been grouped by the key string itself if the key passed to groupBy() didn’t exist. To offer easier checking and sorting of the keys, an empty string is used as key now:

Map::from( [
    10 => ['aid' => 123, 'code' => 'x-abc'],
] )->groupBy( 'xid' );
 
// 1.x
[
    'xid' => [
        10 => ['aid' => 123, 'code' => 'x-abc']
    ]
]
 
// 2.x
[
    '' => [
        10 => ['aid' => 123, 'code' => 'x-abc']
    ]
]

Semantic change in offsetExists()

The offsetExists() method uses isset() instead of array_key_exists() now to be in line with typical PHP behavior. Thus, keys with NULL values are now treated differently:

$m = Map::from( ['foo' => null] );
 
// 1.x
isset( $m['foo'] ); // true
 
// 2.x
isset( $m['foo'] ); // false

Renamed split() to explode()

To avoid conflicts with the Laravel split() method and to be in line with the PHP explode() method, the static Map::split() method has been renamed to Map::explode(). Also, the argument order has changed:

// 1.x
Map::split( 'a,b,c', ',' );
 
// 2.x
Map::explode( ',', 'a,b,c' );

Aimeos 2020.10 LTS release

Aimeos 2020.07 release

The new 2020.10 release with long term support contains a lot of polishing and some features users and developers have been asking for. But one of the most important changes is related to the documentation, which has been greatly improved! Feature-wise, the most important additions are:

  • Complete product rating and review system incl. JSON API
  • Home page component
  • Price facet filter
  • Date/time range filter in admin backend
  • Simplified getter in context
  • Better log entries for SQL statements
  • Official Laravel 8 support
  • Full AWS support
Read more Aimeos 2020.10 LTS release

Aimeos 2020.07 release

Aimeos 2020.07 release

The new Aimeos feature release contains some long-awaited new stuff for developers and customers alike. What you get by using the 2020.07 release among various minor improvements and bugfixes is:

  • PDF invoice in order confirmation e-mail
  • Supplier CSV import
  • Improved admin backend for mobile phones
  • Better full text search and URL segments
  • Official TYPO3 10 support
  • Simplified manager and criteria filter methods
Read more Aimeos 2020.07 release

Aimeos 2020.04 release

2020.04 is the first stable release this year which contains a lot of changes and improvements. The most important one are:

  • Slick and clean frontend theme
  • Better admin interface usability
  • Major performance improvement when using MySQL
  • SEO improvements and support for fractional quantities
  • Cleaned up templates for the HTML client
  • Full SQL Server support
  • Requires PHP 7.1 and PHP Map object
  • Retrieve related items more easily

PHP: Array to Map

PHP Array to Map

Arrays are the working horse of PHP, the programming language used by the vast majority of web applications. An array seems to be incredible simple to use but things get complicated and ugly when PHP functions for array manipulation come into play. Are you tired of hard to read code with excessive error handling too? Then maps can be the answer.

Read more PHP: Array to Map

Aimeos 2019.10 LTS release

The new 2019.10 branch of the Aimeos e-commerce framework with long term support (LTS) contains lots of helpful improvements. The most important ones are:

  • AI-based translation of texts with DeepL
  • Customizable product datasets
  • Manage multiple tax rates
  • Watermarks for images
  • Bulk delete and reorder items
  • Bulk order form for B2B and B2C
  • Add new columns/properties to managers/items easily

Read more Aimeos 2019.10 LTS release

GraphQL vs JSON:API for e-commerce

GraphQL is hot these days! More and more GraphQL APIs appear and especially in the Javascript / NodeJS scene, GraphQL is extremely popular. As progressive web applications using GraphQL are also on the rise (not only in the e-commerce area), we compare how GraphQL and the JSON REST API standard fits for e-commerce applications based on our experience in these areas:

  • Ease of implementation and use
  • Performance and scalability
  • Reading and writing data

Read more GraphQL vs JSON:API for e-commerce

Responsive emails with MJML in Aimeos

Every online shop needs to send e-mails and so the Aimeos e-commerce framework does. They are still the best option to confirm the order and tell customers about changes in the delivery and payment status. Nevertheless, even nowadays, creating appealing e-mails for different devices is hard stuff! Software seem to be from year 2000 when comparing support for HTML5 and CSS in e-mail clients.

Most of the time, support is limited for security reasons which is a good thing because compared to web sites you have to visit actively, everyone can send you e-mails. But often, simple features aren’t implemented since more than ten years or are buggy and therefore unusable. Outlook falls into the later category and it seems that Microsoft wants to punish us forever!

Read more Responsive emails with MJML in Aimeos