PHPackages                             graze/array-merger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. graze/array-merger

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

graze/array-merger
==================

Merge arrays recusively using custom value choosers

0.2(7y ago)754.5k↓47.2%31MITPHPPHP ^5.5 | ^7.0

Since Sep 11Pushed 7y ago11 watchersCompare

[ Source](https://github.com/graze/array-merger)[ Packagist](https://packagist.org/packages/graze/array-merger)[ Docs](https://github.com/graze/array-merger)[ RSS](/packages/graze-array-merger/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (1)

Array Merger
============

[](#array-merger)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2c4ab2106e787ce51e4db6e8d131f640fbcf4943ad259c0bdfd5777ae770b7a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6772617a652f61727261792d6d65726765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/graze/array-merger)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/7bf7ff256c83c221ab644e075e655ec11ae541aec820b7891e8f815167847d5f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6772617a652f61727261792d6d65726765722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/graze/array-merger)[![Coverage Status](https://camo.githubusercontent.com/f65cff6c4af479d214e7091be2e050237b6e9867028eb4531eea2ff77a2fa838/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6772617a652f61727261792d6d65726765722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/graze/array-merger/code-structure)[![Quality Score](https://camo.githubusercontent.com/4227b70d2d8740d9a1b41699f2c2632c0214748824ec3328d1cf7ff951042851/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6772617a652f61727261792d6d65726765722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/graze/array-merger)[![Total Downloads](https://camo.githubusercontent.com/1564dbfcbc7be1afbc2dcf3372db961caae6686208996557877ff02b39b65d3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6772617a652f61727261792d6d65726765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/graze/array-merger)

Array Merger allows you to recursively merge arrays and choose how the values should be merged.

[![Merge](https://camo.githubusercontent.com/846862b05d42f584cf7214b0ffe01310166947e84c28f8f28863742fae7f7a71/68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f636e4558447058766b5a376c6d2f67697068792e676966)](https://camo.githubusercontent.com/846862b05d42f584cf7214b0ffe01310166947e84c28f8f28863742fae7f7a71/68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f636e4558447058766b5a376c6d2f67697068792e676966)

Why
---

[](#why)

The php function: [`array_merge_recursive`](http://php.net/manual/en/function.array-merge-recursive.php)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).

```
$a = ['key' => 'org value', 'key2' => 'first'];
$b = ['key' => 'new value', 'key2' => null];
array_merge_recursive($a, $b);
// ['key' => ['org value', 'new value'], 'key2' => ['first', null]];
```

There is also [`array_replace_recursive`](http://php.net/manual/en/function.array-replace-recursive.php)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.

```
array_replace_recursive($a, $b);
// ['key' => 'new value', 'key2' => null];
```

This library gives you the flexibility to ensure you get the values you actually want in the merge.

```
RecursiveArrayMerger::lastNonNull($a, $b);
// ['key' => 'new value', 'key2' => 'first']);
```

Install
-------

[](#install)

Via Composer

```
composer require graze/array-merger
```

Value Mergers
-------------

[](#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
-----

[](#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.

```
$merger = new Graze\ArrayMerger\ArrayMerger();
$merger->merge(
    ['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
    ['a' => 'second', 'b' => ['d' => 'money']]
);
// ['a' => 'second', 'b' => ['d' => 'money']]

$merger = new Graze\ArrayMerger\RecursiveArrayMerger();
$merger->merge(
    ['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
    ['a' => 'second', 'b' => ['d' => 'money']],
    ['a' => 'third', 'b' => ['e' => 'planets']],
);
// ['a' => 'third', 'b' => ['c' => 'cake', 'd' => 'money', 'e' => 'planets]]
```

### Supplying a Value Merger

[](#supplying-a-value-merger)

By default the last value will be used when merging, however you can supply a different [Value Merger](#value-mergers)to change the behaviour of the merged value.

```
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastNonNullValue());
$merger->merge(
    ['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
    ['a' => 'second', 'b' => ['d' => null]]
);

// ['a' => 'second', 'b' => ['c' => 'cake', 'd' => 'fish']]
```

The Value Merger is a `callable` that can take 2 arguments. This allows you to use in-built and in-line functions:

```
$merger = new Graze\ArrayMerger\RecursiveArrayMerger('max');
$merger->merge(
    ['a' => 1, 'b' => ['c' => 2, 'd' => 3]],
    ['a' => 4, 'b' => ['d' => 1]]
);
// ['a' => 4, 'b' => ['c' => 2, 'd' => 3]]

// or some strange value choose of your choice
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(
    function ($a, $b) {
        return $a % $b == 0 ? $a : $b;
    }
);
$merger->merge(
    ['a' => 1, 'b' => ['c' => 2, 'd' => 3]],
    ['a' => 4, 'b' => ['d' => 1]]
);
// ['a' => 1, 'b' => ['c' => 2, 'd' => 0]]
```

### Calling Statically

[](#calling-statically)

You can call merge statically and specify the Value Merger of your choice:

```
RecursiveArrayMerger::mergeUsing(
    new LastValue(),
    ['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
    ['a' => 'second', 'b' => ['d' => 'money']]
);
// ['a' => 'second', 'b' => ['c' => 'cake', 'd' => 'money']]
```

Each of the supplied value mergers also have static helper methods to call them:

```
RecursiveArrayMerger::lastNonNull(
    ['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
    ['a' => null, 'b' => ['d' => 'money']]
);
// ['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'money']]
```

### Value Arrays

[](#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`.

```
$a = ['a' => 'first', 'b' => ['a','c','d']];
$b = ['a' => 'second', 'b' => ['e']];
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue());
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['a','c','d','e']]

$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue(), RecursiveArrayMerger::FLAG_MERGE_VALUE_ARRAY);
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['e','c','d']]
```

#### Unique Values

[](#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.

```
$a = ['a' => 'first', 'b' => ['a','c','d']];
$b = ['a' => 'second', 'b' => ['d','e']];
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue());
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['a','c','d','d','e']]

$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue(), RecursiveArrayMerger::FLAG_UNIQUE_VALUE_ARRAY);
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['a','c','d','e']]
```

Testing
-------

[](#testing)

```
make build test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Harry Bragg](https://github.com/h-bragg)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

2841d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/637788?v=4)[graze.com](/maintainers/graze)[@graze](https://github.com/graze)

---

Tags

grazearray-merger

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/graze-array-merger/health.svg)

```
[![Health](https://phpackages.com/badges/graze-array-merger/health.svg)](https://phpackages.com/packages/graze-array-merger)
```

###  Alternatives

[bamarni/composer-bin-plugin

No conflicts for your bin dependencies

52823.6M1.0k](/packages/bamarni-composer-bin-plugin)[codercat/jwk-to-pem

Convert JWK to PEM format.

1004.7M26](/packages/codercat-jwk-to-pem)[graze/telnet-client

Telnet client written in PHP

48242.5k4](/packages/graze-telnet-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
