Skip to content

Instantly share code, notes, and snippets.

@bbrothers
Created May 12, 2016 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbrothers/4dc49d22cb6ec96f2f93108765b619d0 to your computer and use it in GitHub Desktop.
Save bbrothers/4dc49d22cb6ec96f2f93108765b619d0 to your computer and use it in GitHub Desktop.
<?php
Collection::macro('diffRecursive', function ($comparate) {
return $this->map(function ($item, $key) use ($comparate) {
$comparate = $this->getArrayableItems($comparate);
if (! isset($comparate[$key])) {
return $item;
}
if (Arr::accessible($item)) {
$item = $this->getArrayableItems($item);
$comparate = $this->getArrayableItems($comparate[$key]);
$diff = array_udiff_assoc($item, $comparate, function ($a, $b) {
if (Arr::accessible($a)) {
return collect($this->getArrayableItems($a))->diffRecursive($b)->all();
}
if ($a == $b) {
return 0;
}
return $a < $b ? -1 : 1;
});
return $diff;
}
return $item == $comparate[$key] ? null : $item;
})->filter();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment