Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays::updateDiff() #246

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Arrays::updateDiff() #246

wants to merge 2 commits into from

Conversation

milo
Copy link
Member

@milo milo commented Jan 7, 2021

  • 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

@JanTvrdik
Copy link
Contributor

So it's like array_diff_assoc but with strict comparision?

@milo
Copy link
Member Author

milo commented Jan 8, 2021

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.

@dg dg force-pushed the master branch 2 times, most recently from 38d3044 to a3ce3eb Compare September 27, 2023 09:53
@dg dg force-pushed the master branch 2 times, most recently from 38cc5db to 0a8a17e Compare October 17, 2023 08:26
@dg dg force-pushed the master branch 5 times, most recently from 4b7b01e to 494d200 Compare November 1, 2023 20:33
@dg dg force-pushed the master branch 5 times, most recently from b843c77 to 77e9645 Compare November 26, 2023 23:02
@dg dg force-pushed the master branch 7 times, most recently from 7ae8df7 to 0d1508c Compare December 5, 2023 14:51
@dg dg force-pushed the master branch 3 times, most recently from 5b5c893 to 81e3e6d Compare January 17, 2024 16:51
@dg dg force-pushed the master branch 2 times, most recently from b080db7 to d668e02 Compare May 3, 2024 11:20
@dg dg force-pushed the master branch 4 times, most recently from 1e73622 to d6a9961 Compare May 16, 2024 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants