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.
boulzy/array-comparator package is a lightweight utility for deep array comparison, making it ideal for:
array_diff, json_encode + json_decode) suffice. Not designed for nested object comparison (requires manual type casting).ArrayComparatorFacade).boulzy/* packages if used.Symfony\Component\Yaml\Yaml::normalize().DateTime, SplObjectStorage) may need pre-processing.ArrayComparator::compare($a, $b, ['ignore_keys' => ['created_at']]).collect($array)->compare()) or a scoped service?assertEquals() (PHPUnit).spatie/array-to-object + assertObjectHasAttributes().league/arrayobjects for immutable comparisons.assertEquals() or mockery for objects.php-diff/php-diff for human-readable deltas.composer.json as a dev dependency:
"require-dev": {
"boulzy/array-comparator": "^1.0"
}
array_diff_recursive calls with the package’s methods.// Before
$diff = array_diff_recursive($expected, $actual);
// After
$comparator = new \Boulzy\ArrayComparator\ArrayComparator();
$diff = $comparator->compare($expected, $actual);
namespace App\Services;
use Boulzy\ArrayComparator\ArrayComparator;
class ArrayComparatorService {
public function __construct(private ArrayComparator $comparator) {}
public function compareArrays(array $a, array $b, array $options = []): array {
return $this->comparator->compare($a, $b, $options);
}
}
AppServiceProvider:
$this->app->singleton(ArrayComparatorService::class, fn() => new ArrayComparatorService(new ArrayComparator()));
call_user_func_array (no closures with static references).composer.json and run composer update.PHPUnit assertions).Cache::remember()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Custom matcher throws exception | Breaks comparison logic | Wrap in try-catch; log errors to Sentry. |
| Large array OOM | Crashes PHP worker (e.g., queues) | Implement chunking or use Symfony\Component\Serializer. |
| PHP version incompatibility | Package fails to load | Pin version in composer.json. |
| Incorrect comparison options | False positives/negatives | Validate options in a DTO or service layer. |
compare(), ignore_keys).assertEquals($expected, $actual, '', true, true, true) alternative).array_key_exists suffices).composer why boulzy/array-comparator.array_diff effectively?").How can I help you explore Laravel packages today?