PHPackages                             myclabs/array-comparator - 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. myclabs/array-comparator

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

myclabs/array-comparator
========================

Array comparator

1.1.0(11y ago)19182.5k↓21.1%3[1 issues](https://github.com/myclabs/ArrayComparator/issues)MITPHPPHP &gt;=5.3.0CI failing

Since Jun 4Pushed 5y ago3 watchersCompare

[ Source](https://github.com/myclabs/ArrayComparator)[ Packagist](https://packagist.org/packages/myclabs/array-comparator)[ Docs](https://github.com/myclabs/ArrayComparator)[ RSS](/packages/myclabs-array-comparator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)DependenciesVersions (7)Used By (0)

ArrayComparator
===============

[](#arraycomparator)

Array comparison helper.

[![Latest Stable Version](https://camo.githubusercontent.com/0f6daf9919599599c9d936f72e2529b8b0f146a53f975b9a5d1a57bcf882e743/68747470733a2f2f706f7365722e707567782e6f72672f6d79636c6162732f61727261792d636f6d70617261746f722f762f737461626c652e706e67)](https://packagist.org/packages/myclabs/array-comparator)[![Build Status](https://camo.githubusercontent.com/48aaeb6f7673e1bda5b6d97636f63cbe7cc777bbab1d935a8a883d5d66782b03/68747470733a2f2f7472617669732d63692e6f72672f6d79636c6162732f4172726179436f6d70617261746f722e706e67)](https://travis-ci.org/myclabs/ArrayComparator)[![Coverage Status](https://camo.githubusercontent.com/983c7bb3458609eebe00b743d26cd74a7922b2ac07116549cccc7241cd187f17/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d79636c6162732f4172726179436f6d70617261746f722f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/myclabs/ArrayComparator?branch=master)

Principle
---------

[](#principle)

Here is the default behavior:

Array 1Array 2Method calledfoo =&gt; Foofoo =&gt; FoowhenEqualbar =&gt; Barbar =&gt; FoowhenDifferentbaz =&gt; BazwhenMissingRightbam =&gt; BamwhenMissingLeftBy default, array keys are compared. This behavior can be customized.

Usage
-----

[](#usage)

```
$comparator = new ArrayComparator();

$comparator->whenEqual(function ($item1, $item2) {
    // Do your stuff !
})
->whenDifferent(function ($item1, $item2) {
    // Do your stuff !
})
->whenMissingRight(function ($item1) {
    // Do your stuff !
})
->whenMissingLeft(function ($item2) {
    // Do your stuff !
});

$comparator->compare($array1, $array2);
```

Advanced example for Doctrine entities for example:

```
$comparator = new ArrayComparator();

// Set that items are considered the same if they have the same id
// Array keys are ignored in this example
$comparator->setItemIdentityComparator(function ($key1, $key2, $item1, $item2) {
    return $item1->id === $item2->id;
});

// Items have differences if their name differ
$comparator->setItemComparator(function ($item1, $item2) {
    return $item1->name === $item2->name;
});

$comparator->whenEqual(function ($item1, $item2) {
    // Do your stuff !
})
->whenDifferent(function ($item1, $item2) {
    // Do your stuff !
})
->whenMissingRight(function ($item1) {
    // Do your stuff !
})
->whenMissingLeft(function ($item2) {
    // Do your stuff !
});

$comparator->compare($array1, $array2);
```

Note that you can also use any PHP callable format instead of inline functions, for example:

```
$comparator->whenDifferent(array($this, 'whenDifferent'));
```

Documentation
-------------

[](#documentation)

- `whenEqual` - Called when 2 items are found in both arrays, and are equal

```
$comparator->whenEqual(function ($item1, $item2) {
});
```

- `whenDifferent` - Called when 2 items are found in both arrays, but are different

```
$comparator->whenDifferent(function ($item1, $item2) {
});
```

- `whenMissingRight` - Called when an item is in the first array (left array) but not in the second (right array)

```
$comparator->whenMissingRight(function ($item1) {
});
```

- `whenMissingLeft` - Called when an item is in the second array (right array) but not in the first (left array)

```
$comparator->whenMissingLeft(function ($item2) {
});
```

- `setItemIdentityComparator` - Overrides the default identity comparator which determine if 2 items represent the same thing

Can be used for example to compare the `id` of the items.

**The default behavior compares the array keys using `===`.**

```
$comparator->setItemIdentityComparator(function ($key1, $key2, $item1, $item2) {
    // return true or false
});
```

- `setItemComparator` - Overrides the default item comparator to determine if 2 items (representing the same thing) have differences

Can be used for example to compare specific attributes of the items. The function should return "is equal", i.e. `true` if items have no differences (then nothing is done because all is good), or `false` if they have differences (then `whenDifferent` is called).

**The default behavior compares the items using `==`.**

```
$comparator->setItemComparator(function ($item1, $item2) {
    // return true or false
});
```

Custom comparator
-----------------

[](#custom-comparator)

There is an alternative to using `setItemIdentityComparator` and `setItemComparator` by writing your own comparator class:

```
class CustomComparator extends ArrayComparator
{
    protected function areSame($key1, $key2, $item1, $item2)
    {
        // Your stuff
        return $item1->id === $item2->id;
    }

    protected function areEqual($item1, $item2)
    {
        // Your stuff
        return $item1->name === $item2->name;
    }
}
```

Installation
------------

[](#installation)

Edit your `composer.json` to add the dependency:

```
{
	"require": {
		"myclabs/array-comparator": "1.0.*"
	}
}
```

ArrayComparator is tested with PHP 5.3 and greater.

Changelog
---------

[](#changelog)

- 1.0: Stable version after testing and use in production (no change from 0.3)
- 0.3: PHP 5.3 compatibility and support all PHP callable types
- 0.2: Allowed to extend the `ArrayComparator` class and write custom comparators
- 0.1: First version

Contribute
----------

[](#contribute)

Install dependencies with Composer:

```
composer install

```

TODO:

- Optimize the array traversals
- Improve documentation ?

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 82.9% of commits — single point of failure

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 ~106 days

Total

5

Last Release

4305d ago

Major Versions

0.3.0 → 1.0.02013-07-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/329a6111724074f5388e95dd41a03ccf3c43f4bfe1ecf27c94c9efc6f7823228?d=identicon)[mnapoli](/maintainers/mnapoli)

![](https://www.gravatar.com/avatar/8220feb8a3d9f1df987987c33494da696aca6929179a6281cdbe45623fcbec0f?d=identicon)[myclabs](/maintainers/myclabs)

---

Top Contributors

[![mnapoli](https://avatars.githubusercontent.com/u/720328?v=4)](https://github.com/mnapoli "mnapoli (34 commits)")[![valentin-claras](https://avatars.githubusercontent.com/u/4446791?v=4)](https://github.com/valentin-claras "valentin-claras (5 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")

---

Tags

arraycomparator

### Embed Badge

![Health badge](/badges/myclabs-array-comparator/health.svg)

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

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[league/config

Define configuration arrays with strict schemas and access values with dot notation

564302.2M24](/packages/league-config)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.2k412.9k11](/packages/aimeos-map)

PHPackages © 2026

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