Map with keys (Flat map)
There are cases when you would like to create a single list of all your matches. flatMap() is great for it.
In other words, it allows you to return one, zero or more elements from your mapping function.
Making a flat map#
Method flatMap() is basically method map(), from which you can return multiple values.
Return types#
flatMap() only accepts an array as its return type. Returning a single element and implicitly creating a one-element
array under the hood would be counter-intuitive and error-prone.
flatMap() accepts only array as its return type.
Variable callbacks#
You can invoke flatMap() with any valid PHP callable, which accepts one or zero string parameters and returns array.
The callable passed to flatMap() must return an array. InvalidReturnValueException is thrown, otherwise.
Mapping with keys#
Method toMap() can be used to return a dictionary, based on matched occurrences.
Detail.offset() returns offset as a valid UTF-8 sequence, whereas preg::match_all()
counts them as bytes. To return bytes number with T-Regx as well,
use byteOffset().
Duplicate keys#
Duplicate keys are not allowed in PHP arrays, so they'll only appear once in the results.