/
intersection.php
41 lines (37 loc) · 1.1 KB
/
intersection.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 present in each 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 difference(), union()
*
* @param iterable|stdClass|null $iterable (variadic) Iterable against which all other passed iterables are compared
* @return array
*
* @example With indexed arrays
Dash\intersection(
[1, 2, 3, 4, 5],
['2', '4'],
[4.0, 2.0]
);
// === [2, 4]
*
* @example With associative arrays
Dash\intersection(
['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5],
['a' => 2, 'b' => 4],
['a' => 4, 'b' => 2]
);
// === ['b' => 2, 'd' => 4]
*/
function intersection($iterable /*, ...iterables */)
{
assertType($iterable, ['iterable', 'stdClass', 'null'], __FUNCTION__);
$args = map(func_get_args(), 'Dash\toArray');
$args[] = 'Dash\compare';
$intersection = call_user_func_array('array_uintersect', $args);
return isIndexedArray($iterable) ? array_values($intersection) : $intersection;
}