Skip to content

Conversation

MohannadNaj
Copy link
Contributor

This PR introduces a Collection static function for using array_diff_assocon collections.

Using this suggested function diffAssoc, we can compare collections with respect to both keys and values.

From the PHP documentation:

array_diff_assoc ( array $array1 , array $array2 )
Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are also used in the comparison.

Usage: diff and diffKeys and diffAssoc:

$c1 = collect(
['id' => 1,
'username' => 'john',
'is_active' => 1,
'prefer_sms' => 1
]);

$c2 = collect(
	['id' => 2,
	'username' => 'doe',
	'is_active' => 1,
	]);

$c1->diff($c2);
//  ['username' => 'john']
//  the 'id' difference is ignored because the value of the 'id' index in the first collection, which is '1', is present in the 'is_active' index in the second collection.

$c1->diffKeys($c2);
//  ['prefer_sms' => 1]

$c1->diffAssoc($c2);
//  ['id' => 1, 'username' => 'john' , 'prefer_sms' => 1]

@taylorotwell taylorotwell merged commit 3dbe83f into laravel:5.4 Jun 15, 2017
mathieutu added a commit to mathieutu/framework that referenced this pull request Jun 19, 2017
* commit '05060b183dd09cee6dce498da5afde9e66fd1a29':
  Apply fixes from StyleCI (laravel#19659)
  [5.4] Add ability to remove a global scope with another global scope (laravel#19657)
  [5.4] Add fresh method on Eloquent\Collection. (laravel#19616)
  support multiple fields to validateDifferent (laravel#19637)
  escape default value of yield blade directive (laravel#19643)
  [5.4] Add "avg" and "average" to higher order proxy. (laravel#19628)
  tagged v5.4.27 release notes
  version
  Add missing dependency. (laravel#19623)
  update timestamp on soft delete only when its used (laravel#19627)
  Add new `diffAssoc()` method to collection (laravel#19604)
  Fix tests code coverage config. (laravel#19609)

# Conflicts:
#	src/Illuminate/Database/Eloquent/Collection.php
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.

2 participants