-
Notifications
You must be signed in to change notification settings - Fork 15
/
StrictArrayComparer.php
52 lines (43 loc) · 1.13 KB
/
StrictArrayComparer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare( strict_types = 1 );
namespace Diff\ArrayComparer;
/**
* Strict variant of PHPs array_diff method.
*
* Similar to @see array_diff with the following differences:
*
* - Strict comparison for arrays: ['42'] and [42] are different
* - Quantity matters: [42, 42] and [42] are different
* - Arrays and objects are compared properly: [[1]] and [[2]] are different
* - Naive support for objects by using non-strict comparison
* - Only works with two arrays (array_diff can take more)
*
* @since 0.8
*
* @license BSD-3-Clause
* @author Jeroen De Dauw < [email protected] >
*/
class StrictArrayComparer implements ArrayComparer {
/**
* @see ArrayComparer::diffArrays
*
* @since 0.8
*
* @param array $arrayOne
* @param array $arrayTwo
*
* @return array
*/
public function diffArrays( array $arrayOne, array $arrayTwo ): array {
$notInTwo = [];
foreach ( $arrayOne as $element ) {
$location = array_search( $element, $arrayTwo, !is_object( $element ) );
if ( $location === false ) {
$notInTwo[] = $element;
continue;
}
unset( $arrayTwo[$location] );
}
return $notInTwo;
}
}