Download the PHP package xpaw/compare-arrays without Composer
On this page you can find all versions of the php package xpaw/compare-arrays. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xpaw/compare-arrays
More information about xpaw/compare-arrays
Files in xpaw/compare-arrays
Package compare-arrays
Short Description Diffing multi dimensional arrays the easy way
License MIT
Homepage https://github.com/xPaw/CompareArrays.php
Informations about the package compare-arrays
CompareArrays
A PHP library for comparing multi-dimensional arrays and detecting differences.
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
Install via Composer:
Usage
Basic Comparison
Result:
Flattening Results
To simplify handling of nested differences, you can flatten the result:
Result:
Custom Separator and Path Prefix
You can customize the separator and add a path prefix when flattening:
Result:
API Reference
CompareArrays::Diff(array $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
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
Each detected difference is represented by a ComparedValue
object with:
$Type
: One ofadded
,removed
, ormodified
$OldValue
: The value from the old array (null for added items)$NewValue
: The value from the new array (null for removed items)
Special Cases
Float Comparison
Floating-point values are compared with PHP_FLOAT_EPSILON
to handle precision issues:
License
This library is licensed under the MIT License.