-
Notifications
You must be signed in to change notification settings - Fork 8
/
difference.php
41 lines (37 loc) · 1.08 KB
/
difference.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
<?php
namespace Dash;
/**
* Returns the set of elements from `$iterable` whose values are not present in any of the other iterables,
* where values are compared using loose equality.
*
* The order, keys, and values of elements in the returned array are determined by `$iterable`.
*
* @see intersection(), union()
*
* @param iterable|stdClass|null $iterable (variadic) Iterable against which all other passed iterables are compared
* @return array
*
* @example With indexed arrays
Dash\difference(
[1, 2, 3, 4, 5],
['2', 4],
[3.0, 4]
);
// === [1, 5]
*
* @example With associative arrays
Dash\difference(
['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5],
['a' => '2', 'b' => 4],
['a' => 3.0, 'b' => 4]
);
// === ['a' => 1, 'e' => 5]
*/
function difference($iterable /*, ...iterables */)
{
assertType($iterable, ['iterable', 'stdClass', 'null'], __FUNCTION__);
$args = map(func_get_args(), 'Dash\toArray');
$args[] = 'Dash\compare';
$difference = call_user_func_array('array_udiff', $args);
return isIndexedArray($iterable) ? array_values($difference) : $difference;
}