Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Array Comparator

Array Comparator Laravel Package

boulzy/array-comparator

Compare PHP arrays with boulzy/array-comparator: detect differences and changes between two arrays, including nested structures. Useful for testing, syncing data, and debugging by quickly showing what was added, removed, or modified.

View on GitHub
Deep Wiki
Context7

A library to compare arrays using matching functions

Frequently asked questions about Array Comparator
How do I install boulzy/array-comparator in a Laravel project?
Add it via Composer as a dev dependency: `composer require-dev boulzy/array-comparator`. For production use, omit `dev` and ensure PHP 7.4+ is supported. The package is stateless and requires no Laravel-specific setup.
Can this package handle nested arrays and detect changes at any depth?
Yes, it performs deep comparison by default, including nested arrays. Use options like `ignore_keys` to exclude specific keys (e.g., timestamps) from comparison. For custom logic, pass a callback to the `compare` method.
Does boulzy/array-comparator work with Laravel’s testing tools like PHPUnit?
Absolutely. It’s perfect for assertions in PHPUnit tests—replace manual `assertEquals()` with `$comparator->compare($expected, $actual)` to get detailed diffs. Works alongside Laravel’s built-in testing helpers without conflicts.
What Laravel versions does this package support?
The package itself is framework-agnostic but requires PHP 7.4+. It integrates seamlessly with Laravel 8+ (or 7.x with PHP 7.4). No Laravel-specific features mean it won’t break across minor versions.
How do I ignore certain keys (e.g., auto-generated IDs) during comparison?
Pass an `ignore_keys` option to the `compare` method: `$comparator->compare($a, $b, ['ignore_keys' => ['id', 'created_at']])`. This skips those keys entirely, treating them as always equal.
Is boulzy/array-comparator faster than `array_diff_recursive` for large arrays?
It depends on the array size and structure. For small to medium arrays (<10,000 elements), performance is comparable. For larger datasets, benchmark against `array_diff_recursive` or consider caching hashes if comparisons are repeated.
Can I use this for comparing database records or API responses in Laravel?
Yes, it’s ideal for this. Fetch records as arrays (e.g., with Eloquent’s `toArray()`) and compare them to detect changes. Useful for syncing data, validating API payloads, or auditing database modifications.
Are there alternatives to boulzy/array-comparator in Laravel?
For testing, Laravel’s native `assertEquals()` or PHPUnit’s methods suffice. For diffing, consider `php-diff/php-diff` for human-readable output. For immutable objects, `league/arrayobjects` is a stronger fit. Choose this package for deep array comparison with customizable matching.
How do I handle custom comparison logic (e.g., comparing DateTime objects)?
Pre-process your arrays to convert objects to strings or arrays before comparison, or use the `matcher` option to pass a custom callback. Example: `$comparator->compare($a, $b, ['matcher' => fn($x, $y) => $x->getTimestamp() === $y->getTimestamp()])`.
Should I wrap this in a Laravel service class for reusability?
Yes, for consistency. Create a service class (e.g., `ArrayComparatorService`) that extends the package’s functionality, then bind it in `AppServiceProvider`. This centralizes usage and allows dependency injection in controllers/tests.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed