fortress / array-comparison
Compare two arrays and return differences
1.0.0
2022-07-22 15:26 UTC
Requires
- php: ^8.1
Requires (Dev)
- infection/infection: ^0.26.13
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^8.3
- squizlabs/php_codesniffer: ^3.7
README
Compare two associative arrays and see the difference between them.
Supports nested associative arrays and array collections.
Installation
You can install the package via composer:
composer require fortress/array-comparison
Usage
$expected = [ 'object' => [ 'to_be_removed' => [ 'item1', 'item2', ], 'to_be_changed' => [ 'item3', 'item4', ] ] ]; $actual = [ 'object' => [ 'to_be_added' => [ 'item3', 'item4', ], 'to_be_changed' => [ 'item5', 'item6', ], ] ]; $comparison = new Fortress\ArrayComparison(); $comparison->getDiff($expected, $actual);
Results in:
[
'added' => [
'object' => [
'to_be_added' => [
'item3',
'item4',
],
]
],
'removed' => [
'object' => [
'to_be_removed' => [
'item1',
'item2',
],
]
],
'changed' => [
'object' => [
'to_be_changed' => [
'old' => [
'item3',
'item4',
],
'new' => [
'item5',
'item6',
],
],
],
],
]
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.