fortress/array-comparison

Compare two arrays and return differences

1.0.0 2022-07-22 15:26 UTC

This package is auto-updated.

Last update: 2024-12-22 20:44:02 UTC


README

Latest Version on Packagist Tests

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.