/
union.php
39 lines (35 loc) · 995 Bytes
/
union.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
<?php
namespace Dash;
/**
* Returns a new array containing the combined set of unique values, in order, of all provided iterables.
*
* Non-indexed keys are preseved, but duplicate keys will overwrite previous ones.
*
* @see intersection(), difference()
*
* @param iterable|stdClass|null $iterable (variadic) One or more iterables to merge
* @return array
*
* @example With indexed arrays
Dash\union(
[1, 3, 5],
[2, 4, 6],
[7, 8]
);
// === [1, 3, 5, 2, 4, 6, 7, 8]
*
* @example With associative arrays
Dash\union(
['a' => 1, 'c' => 3],
['b' => 2, 'd' => 4],
['e' => 5, 'f' => 6]
);
// === ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6]
*/
function union($iterable /*, ...iterables */)
{
assertType($iterable, ['iterable', 'stdClass', 'null'], __FUNCTION__);
$values = map(func_get_args(), 'Dash\toArray');
$union = array_unique(call_user_func_array('array_merge', $values));
return isIndexedArray($iterable) ? array_values($union) : $union;
}