utils icon indicating copy to clipboard operation
utils copied to clipboard

Arrays::updateDiff()

Open milo opened this issue 4 years ago • 2 comments

  • new feature
  • BC break? no
  • doc PR: will

Proposed Arrays::updateDiff() compares two associative arrays and returns such items from later one, which does not exist or differs from first one. By other words - which items have to be updated in first array, to be the same as second array.

As far as I know, there is no PHP function for that (playground). For examle:

$from = ['a' => null];
$to = ['a' => false];

# I didn't find a PHP function which returns
$diff = ['a' => false];

There are posible things to debate:

  • function name
  • arguments order
  • recursive version
  • objects comparing
  • 3rd argument callable $comparator = null

milo avatar Jan 07 '21 20:01 milo

So it's like array_diff_assoc but with strict comparision?

JanTvrdik avatar Jan 07 '21 23:01 JanTvrdik

Yes. Behaviour probably same as:

return array_udiff_assoc($to, $from, function ($a, $b) {
	return $a === $b ? 0 : ($a > $b ? 1 : -1);  # spaceship cannot be used because null <=> false === 0
});

I write probably, because I'm not sure why yes/no comparator can return -1/0/1 and how result depends on it. And the foreach loop is ~3 times faster.

milo avatar Jan 08 '21 08:01 milo