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 Diff Multidimensional Laravel Package

rogervila/array-diff-multidimensional

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require rogervila/array-diff-multidimensional
    

    No additional configuration is required—just autoload the package.

  2. Basic Usage

    use Rogervila\ArrayDiffMultidimensional\ArrayDiff;
    
    $array1 = ['a' => 1, 'b' => ['x' => 10, 'y' => 20]];
    $array2 = ['a' => 1, 'b' => ['x' => 10, 'y' => 30]];
    
    $diff = ArrayDiff::diff($array1, $array2);
    // Returns: ['b' => ['y' => 30]]
    
  3. First Use Case Compare two nested API responses or database records to identify changes:

    $oldUser = User::find(1)->toArray();
    $newUser = $request->validate()->merge($oldUser)->toArray();
    $changes = ArrayDiff::diff($oldUser, $newUser);
    

Implementation Patterns

Common Workflows

  1. Change Detection in Laravel Use with Eloquent models to track field changes:

    $original = $model->getOriginal();
    $changes = ArrayDiff::diff($original, $model->toArray());
    
  2. Deep Merging with Conditional Logic Combine with array_merge_recursive to preserve unchanged values:

    $merged = array_merge_recursive(
        $array1,
        ArrayDiff::diff($array1, $array2)
    );
    
  3. Diff for Form Validation Validate nested form submissions against a base structure:

    $validated = $request->validate(['data' => 'required|array']);
    $diff = ArrayDiff::diff($baseConfig, $validated['data']);
    if (!empty($diff)) {
        throw new \Exception("Invalid nested fields");
    }
    
  4. Integration with Laravel Collections Process diffs in bulk:

    $users = User::all();
    $diffs = $users->map(fn ($user) => ArrayDiff::diff(
        $user->getOriginal(),
        $user->toArray()
    ));
    

Performance Tips

  • Cache Results: Store diffs in the session or cache for repeated comparisons.
  • Limit Depth: Use ArrayDiff::diff($array1, $array2, 3) to restrict recursion depth for large arrays.

Gotchas and Tips

Pitfalls

  1. Key Order Sensitivity The package preserves key order from the first array. If order matters, ensure consistency:

    ksort($array1); ksort($array2); // Normalize before diffing
    
  2. Non-Array Values Passing non-array inputs throws InvalidArgumentException. Validate inputs:

    if (!is_array($array1) || !is_array($array2)) {
        throw new \InvalidArgumentException("Inputs must be arrays");
    }
    
  3. Circular References Infinite recursion occurs with circular references. Use ArrayDiff::diff($array1, $array2, null, true) to detect cycles.

  4. Type Juggling Values like 0 (integer) vs. '0' (string) are treated as equal. Use strict comparison if needed:

    $diff = ArrayDiff::diff($array1, $array2, null, false, true); // Strict mode
    

Debugging

  • Verbose Output: Enable debug mode for detailed diffs:
    $diff = ArrayDiff::diff($array1, $array2, null, false, false, true);
    
  • Log Diffs: Use Laravel’s logging to inspect complex diffs:
    \Log::debug('Array Diff:', ['diff' => $diff]);
    

Extension Points

  1. Custom Diff Strategies Override the default comparator:

    ArrayDiff::setComparator(function ($oldVal, $newVal) {
        return $oldVal === $newVal || (is_numeric($oldVal) && is_numeric($newVal));
    });
    
  2. Event Hooks Extend with events (e.g., pre/post-diff hooks) via traits or decorators.

  3. Laravel Service Provider Bind the diff service for dependency injection:

    $this->app->singleton(ArrayDiff::class, function () {
        return new ArrayDiff();
    });
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware