Download the PHP package graze/array-merger without Composer
On this page you can find all versions of the php package graze/array-merger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download graze/array-merger
More information about graze/array-merger
Files in graze/array-merger
Package array-merger
Short Description Merge arrays recusively using custom value choosers
License MIT
Homepage https://github.com/graze/array-merger
Informations about the package array-merger
Array Merger
Array Merger allows you to recursively merge arrays and choose how the values should be merged.
Why
The php function: array_merge_recursive
does indeed merge arrays, but it converts values with duplicate keys to arrays rather than overwriting the value in
the first array with the duplicate value in the second array, as array_merge does. I.e., with array_merge_recursive,
this happens (documented behaviour).
There is also array_replace_recursive
which replaces values in the first with values in the second, but it handles value arrays differently and only supports
replacing with the last value.
This library gives you the flexibility to ensure you get the values you actually want in the merge.
Install
Via Composer
Value Mergers
- LastValue: Takes the last value (default) _equivalent to
array_replace_recursive
_ - LastNonNullValue: Takes the last value, unless it is null then the first
- FirstValue: Takes the first value
- FirstNonNullValue: Takes the first value, unless it is null, then the second
- RandomValue: Takes a random value
- SumValue: If both values are numeric, will add them together
- ProductValue: If both values are numeric, will multiply them together
- BothValues: Will return both values in an array, _equivalent to
array_merge_recursive
_
Usage
There is an ArrayMerger
which will only merge at the top level, and a RecursiveArrayMerger
which will merge
recursively.
The mergers can take any number of arguments, and will treat the first argument as the base array to merge the subsequent arrays into.
Supplying a Value Merger
By default the last value will be used when merging, however you can supply a different Value Merger to change the behaviour of the merged value.
The Value Merger is a callable
that can take 2 arguments. This allows you to use in-built and in-line functions:
Calling Statically
You can call merge statically and specify the Value Merger of your choice:
Each of the supplied value mergers also have static helper methods to call them:
Value Arrays
By default value arrays (arrays with no indexes supplied) will be appended onto each other, if you want to treat them
as associative arrays, you can supply this flag: RecursiveArrayMerger::FLAG_MERGE_VALUE_ARRAY
.
Unique Values
By default, when we append value arrays it will keep duplicate values. If you want to remove the duplicate values you
can supply the flag: RecursiveArrayMerger::FLAG_UNIQUE_VALUE_ARRAY
.
This flag will only be relevant if the RecursiveArrayMerger::FLAG_MERGE_VALUE_ARRAY
flag is not supplied.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Harry Bragg
- All Contributors
License
The MIT License (MIT). Please see License File for more information.