PHPackages                             xpaw/compare-arrays - 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. xpaw/compare-arrays

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

xpaw/compare-arrays
===================

Diffing multi dimensional arrays the easy way

3.0.0(1y ago)91.4k2MITPHPPHP &gt;=8.1CI passing

Since Oct 15Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/xPaw/CompareArrays.php)[ Packagist](https://packagist.org/packages/xpaw/compare-arrays)[ Docs](https://github.com/xPaw/CompareArrays.php)[ RSS](/packages/xpaw-compare-arrays/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

CompareArrays
=============

[](#comparearrays)

[![Latest Stable Version](https://camo.githubusercontent.com/613886ef6c4230d48dd974b5fa565c541b31e1ffdfc47bf0be17f1ead8172c50/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f787061772f636f6d706172652d6172726179732e737667)](https://packagist.org/packages/xpaw/compare-arrays)[![License](https://camo.githubusercontent.com/58ddc8609fe8babb769473bece2d8f0f088fddb68656bea42e7c6c08da795098/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f785061772f436f6d706172654172726179732e7068702e737667)](https://github.com/xPaw/CompareArrays.php/blob/master/LICENSE)[![PHP Version](https://camo.githubusercontent.com/6dfd47473a5c181d5fb5d743ae29074598cdcc59b680d4906b5f750f03c040d7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f787061772f636f6d706172652d6172726179732e737667)](https://packagist.org/packages/xpaw/compare-arrays)

A PHP library for comparing multi-dimensional arrays and detecting differences.

Features
--------

[](#features)

- Deep comparison of multi-dimensional arrays
- Detects added, removed, and modified values
- Maintains array structure in the result
- Special handling for float comparison with epsilon
- Flatten results into a single-dimensional array with path keys

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

[](#installation)

Install via Composer:

```
composer require xpaw/compare-arrays
```

Usage
-----

[](#usage)

### Basic Comparison

[](#basic-comparison)

```
use xPaw\CompareArrays\CompareArrays;

$oldArray = [
	'user' => [
		'name' => 'John',
		'age' => 30,
		'settings' => [
			'darkMode' => true,
			'notifications' => true
		]
	]
];

$newArray = [
	'user' => [
		'name' => 'John Doe',
		'age' => 30,
		'settings' => [
			'darkMode' => true,
			'notifications' => false
		],
		'lastLogin' => '2025-03-20'
	]
];

$differences = CompareArrays::Diff($oldArray, $newArray);
print_r($differences);
```

Result:

```
Array(
    [user] => Array(
		[name] => xPaw\CompareArrays\ComparedValue Object(
			[OldValue] => John
			[NewValue] => John Doe
			[Type] => modified
		)

		[settings] => Array(
			[notifications] => xPaw\CompareArrays\ComparedValue Object(
				[OldValue] => 1
				[NewValue] =>
				[Type] => modified
			)
		)

		[lastLogin] => xPaw\CompareArrays\ComparedValue Object(
			[OldValue] =>
			[NewValue] => 2025-03-20
			[Type] => added
		)
	)
)

```

### Flattening Results

[](#flattening-results)

To simplify handling of nested differences, you can flatten the result:

```
$flattened = CompareArrays::Flatten($differences);
print_r($flattened);
```

Result:

```
Array(
    [user/name] => xPaw\CompareArrays\ComparedValue Object(
		[OldValue] => John
		[NewValue] => John Doe
		[Type] => modified
	)

    [user/settings/notifications] => xPaw\CompareArrays\ComparedValue Object(
		[OldValue] => 1
		[NewValue] =>
		[Type] => modified
	)

    [user/lastLogin] => xPaw\CompareArrays\ComparedValue Object(
		[OldValue] =>
		[NewValue] => 2025-03-20
		[Type] => added
	)
)

```

### Custom Separator and Path Prefix

[](#custom-separator-and-path-prefix)

You can customize the separator and add a path prefix when flattening:

```
// Using a dot as separator
$flattened = CompareArrays::Flatten($differences, '.');
```

Result:

```
Array
(
    [user.name] => xPaw\CompareArrays\ComparedValue Object(...)
    [user.settings.notifications] => xPaw\CompareArrays\ComparedValue Object(...)
    [user.lastLogin] => xPaw\CompareArrays\ComparedValue Object(...)
)

```

API Reference
-------------

[](#api-reference)

### CompareArrays::Diff(array $Old, array $New): array

[](#comparearraysdiffarray-old-array-new-array)

Compares two arrays and produces a new array of changes between these arrays.

- The result maintains the same structure as the input arrays
- The deepest values are `ComparedValue` objects
- Float values are compared with `PHP_FLOAT_EPSILON` for precision
- Special handling for arrays vs non-arrays in corresponding positions

### CompareArrays::Flatten(array $Input, string $Separator = '/', ?string $Path = null): array

[](#comparearraysflattenarray-input-string-separator---string-path--null-array)

Flattens a multi-dimensional array into a one-dimensional array.

- Keys are transformed into paths separated by the specified separator
- Optionally prepend a path prefix to all keys

### ComparedValue

[](#comparedvalue)

Each detected difference is represented by a `ComparedValue` object with:

- `$Type`: One of `added`, `removed`, or `modified`
- `$OldValue`: The value from the old array (null for added items)
- `$NewValue`: The value from the new array (null for removed items)

Special Cases
-------------

[](#special-cases)

### Float Comparison

[](#float-comparison)

Floating-point values are compared with `PHP_FLOAT_EPSILON` to handle precision issues:

```
$differences = CompareArrays::Diff(
	['value' => 0.1],
	['value' => 0.1 + 0.00000001]
);
// Result: empty array (no differences)

$differences = CompareArrays::Diff(
	['value' => 0.1],
	['value' => 0.1 + 0.0001]
);
// Result: detects difference
```

License
-------

[](#license)

This library is licensed under the [MIT License](LICENSE).

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance68

Regular maintenance activity

Popularity27

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 91.4% 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 ~991 days

Total

3

Last Release

424d ago

Major Versions

1.0.0 → 2.0.02019-10-26

2.0.0 → 3.0.02025-03-20

PHP version history (3 changes)1.0.0PHP &gt;=7.1

2.0.0PHP &gt;=7.2

3.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e0942af20ff43fb658f834cff0348fe0b2c5d153b7fa9bef0938591edba13fe?d=identicon)[xPaw](/maintainers/xPaw)

---

Top Contributors

[![xPaw](https://avatars.githubusercontent.com/u/613331?v=4)](https://github.com/xPaw "xPaw (32 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

phpdiffarraycomparemulti-dimensional

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/xpaw-compare-arrays/health.svg)

```
[![Health](https://phpackages.com/badges/xpaw-compare-arrays/health.svg)](https://phpackages.com/packages/xpaw-compare-arrays)
```

###  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)[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31052.5M47](/packages/openlss-lib-array2xml)

PHPackages © 2026

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