1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funct\Collection; |
4
|
|
|
|
5
|
|
|
use Traversable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Returns a copy of the array with all falsy values removed |
9
|
|
|
* |
10
|
|
|
* @param array $collection |
11
|
|
|
* |
12
|
|
|
* @return array |
13
|
|
|
* @author Aurimas Niekis <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
function compact($collection) |
16
|
|
|
{ |
17
|
4 |
|
return array_filter($collection); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Sorts a array into groups and returns a count for the number of objects in each group. Similar to groupBy, but |
22
|
|
|
* instead of returning a array of values, returns a count for the number of values in that group. |
23
|
|
|
* |
24
|
|
|
* @param array $collection |
25
|
|
|
* @param callable|string $callback |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
* @author Aurimas Niekis <[email protected]> |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
function countBy($collection, $callback) |
31
|
|
|
{ |
32
|
2 |
|
$result = []; |
33
|
|
|
|
34
|
2 |
|
foreach ($collection as $key => $value) { |
35
|
2 |
|
if (is_callable($callback)) { |
36
|
1 |
|
$groupName = call_user_func($callback, $value); |
37
|
1 |
|
} else { |
38
|
1 |
|
$groupName = $value[$callback]; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
$result[$groupName] = get($result, $groupName, 0); |
42
|
2 |
|
$result[$groupName]++; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
2 |
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns true if all of the values in the array pass the callback truth test. |
51
|
|
|
* |
52
|
|
|
* @author Aurimas Niekis <[email protected]> |
53
|
|
|
* |
54
|
|
|
* @param array $collection |
55
|
|
|
* @param callable $callback |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
function every($collection, callable $callback = null) |
60
|
|
|
{ |
61
|
5 |
|
if (null === $callback) { |
62
|
|
|
$callback = function ($item) use ($callback) { |
63
|
3 |
|
return (true == $item); |
64
|
3 |
|
}; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
5 |
|
return count( |
68
|
5 |
|
array_filter( |
69
|
5 |
|
$collection, |
70
|
|
|
function ($item) use ($callback) { |
71
|
5 |
|
return false === call_user_func($callback, $item); |
72
|
|
|
} |
73
|
5 |
|
) |
74
|
5 |
|
) < 1; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Looks through the array and returns the first value that matches all of the key-value pairs listed in properties. |
80
|
|
|
* |
81
|
|
|
* @param array $collection |
82
|
|
|
* @param array $value |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
* @author Aurimas Niekis <[email protected]> |
86
|
|
|
*/ |
87
|
|
|
function findWhere($collection, $value) |
88
|
|
|
{ |
89
|
2 |
View Code Duplication |
foreach ($collection as $key => $item) { |
90
|
2 |
|
$diff = array_diff_assoc($value, $item); |
91
|
|
|
|
92
|
2 |
|
if (count($diff) < 1) { |
93
|
1 |
|
return $item; |
94
|
|
|
} |
95
|
2 |
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array|Traversable $collection |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
function first($collection) |
104
|
|
|
{ |
105
|
1 |
|
return reset($collection); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array|Traversable $collection |
110
|
|
|
* @param int $n First n elements of array |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
function firstN($collection, $n = 1) |
115
|
|
|
{ |
116
|
1 |
|
return array_slice($collection, 0, $n); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Flattens a nested array by depth. |
121
|
|
|
* |
122
|
|
|
* @param array $collection |
123
|
|
|
* @param int $depth |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
* @author Aurimas Niekis <[email protected]> |
127
|
|
|
*/ |
128
|
|
|
function flatten($collection, $depth = 1) |
129
|
|
|
{ |
130
|
5 |
|
$result = []; |
131
|
|
|
|
132
|
5 |
|
foreach ($collection as $value) { |
133
|
5 |
|
if (is_array($value) && $depth > 0) { |
134
|
5 |
|
$result = array_merge($result, flatten($value, --$depth)); |
135
|
5 |
|
} else { |
136
|
5 |
|
$result[] = $value; |
137
|
|
|
} |
138
|
5 |
|
} |
139
|
|
|
|
140
|
5 |
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Flattens all arrays to single level |
145
|
|
|
* |
146
|
|
|
* @param array $collection |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
* @author Aurimas Niekis <[email protected]> |
150
|
|
|
*/ |
151
|
|
|
function flattenAll($collection) |
152
|
|
|
{ |
153
|
5 |
|
$result = []; |
154
|
|
|
|
155
|
5 |
|
foreach ($collection as $value) { |
156
|
5 |
|
if (is_array($value)) { |
157
|
5 |
|
$result = array_merge($result, flattenAll($value)); |
158
|
5 |
|
} else { |
159
|
5 |
|
$result[] = $value; |
160
|
|
|
} |
161
|
5 |
|
} |
162
|
|
|
|
163
|
5 |
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Runs a callback for every value in collection with value as first argument and additional arguments and returns |
169
|
|
|
* array of results |
170
|
|
|
* |
171
|
|
|
* @param array|Traversable $collection |
172
|
|
|
* @param callable $callable |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
* @author Aurimas Niekis <[email protected]> |
176
|
|
|
*/ |
177
|
|
|
function forEvery($collection, $callable) |
|
|
|
|
178
|
|
|
{ |
179
|
2 |
|
return call_user_func_array('Funct\\Collection\\invoke', func_get_args()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns item from collection if exists otherwise null or default value |
185
|
|
|
* |
186
|
|
|
* @author Aurimas Niekis <[email protected]> |
187
|
|
|
* |
188
|
|
|
* @param array|Traversable $collection |
189
|
|
|
* @param string $key |
190
|
|
|
* @param mixed $default |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
|
|
function get($collection, $key, $default = null) |
195
|
|
|
{ |
196
|
11 |
|
if (isset($collection[$key])) { |
197
|
11 |
|
return $collection[$key]; |
198
|
|
|
} else { |
199
|
7 |
|
return $default; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Splits a collection into sets, grouped by the result of running each value through callback. If callback is a string |
206
|
|
|
* instead of a function, groups by the property named by callback on each of the values. |
207
|
|
|
* |
208
|
|
|
* @param array $collection |
209
|
|
|
* @param callable|string $callback |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
* @author Aurimas Niekis <[email protected]> |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
function groupBy($collection, $callback) |
215
|
|
|
{ |
216
|
2 |
|
$result = []; |
217
|
|
|
|
218
|
2 |
|
foreach ($collection as $key => $value) { |
219
|
2 |
|
if (is_callable($callback)) { |
220
|
1 |
|
$groupName = call_user_func($callback, $value); |
221
|
1 |
|
} else { |
222
|
1 |
|
$groupName = $value[$callback]; |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
$result[$groupName] = get($result, $groupName, []); |
226
|
2 |
|
$result[$groupName][$key] = $value; |
227
|
2 |
|
} |
228
|
|
|
|
229
|
2 |
|
return $result; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the |
235
|
|
|
* last n elements from the result. |
236
|
|
|
* |
237
|
|
|
* @param array $collection |
238
|
|
|
* @param int $n |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
* @author Aurimas Niekis <[email protected]> |
242
|
|
|
*/ |
243
|
|
|
function initial($collection, $n = 1) |
244
|
|
|
{ |
245
|
3 |
|
return array_slice($collection, 0, -$n); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each |
251
|
|
|
* of the arrays. |
252
|
|
|
* |
253
|
|
|
* @param array $collectionFirst |
254
|
|
|
* @param array $collectionSecond |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
* @author Aurimas Niekis <[email protected]> |
258
|
|
|
*/ |
259
|
|
|
function intersection($collectionFirst, $collectionSecond) |
|
|
|
|
260
|
|
|
{ |
261
|
2 |
|
return call_user_func_array('array_intersect', func_get_args()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Invokes callback on each value in the list. Any extra arguments passed will be forwarded on to the method invocation. |
267
|
|
|
* |
268
|
|
|
* @param array $collection |
269
|
|
|
* @param callable $callback |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
* @author Aurimas Niekis <[email protected]> |
273
|
|
|
*/ |
274
|
|
|
function invoke($collection, callable $callback) |
275
|
|
|
{ |
276
|
4 |
|
$arguments = func_get_args(); |
277
|
|
|
|
278
|
4 |
|
return array_map( |
279
|
|
|
function ($item) use ($callback, $arguments) { |
280
|
4 |
|
$arguments = array_merge([$item], array_slice($arguments, 2)); |
281
|
|
|
|
282
|
4 |
|
return call_user_func_array($callback, $arguments); |
283
|
4 |
|
}, |
284
|
|
|
$collection |
285
|
4 |
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns last element of array |
291
|
|
|
* |
292
|
|
|
* @param array|Traversable $collection |
293
|
|
|
* |
294
|
|
|
* @return array |
295
|
|
|
*/ |
296
|
|
|
function last($collection) |
297
|
|
|
{ |
298
|
1 |
|
return end($collection); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the index of the last occurrence of value in the array, or false if value is not present |
304
|
|
|
* |
305
|
|
|
* @param array $collection |
306
|
|
|
* @param mixed $value |
307
|
|
|
* |
308
|
|
|
* @return int|bool |
309
|
|
|
* @author Aurimas Niekis <[email protected]> |
310
|
|
|
*/ |
311
|
|
|
function lastIndexOf($collection, $value) |
312
|
|
|
{ |
313
|
1 |
|
$result = array_keys($collection, $value); |
314
|
|
|
|
315
|
1 |
|
if (count($result) < 1) { |
316
|
1 |
|
return false; |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
return end($result); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Returns the last element of an array. Passing n will return the last n elements of the array. |
325
|
|
|
* |
326
|
|
|
* @param array|Traversable $collection |
327
|
|
|
* @param int $n Last n elements of array |
328
|
|
|
* |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
|
|
function lastN($collection, $n = 1) |
332
|
|
|
{ |
333
|
1 |
|
return array_slice($collection, (-1 * $n)); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Returns the maximum value in collection using callback method |
339
|
|
|
* |
340
|
|
|
* @param array $collection |
341
|
|
|
* @param callable $callback |
342
|
|
|
* |
343
|
|
|
* @return array |
344
|
|
|
* @author Aurimas Niekis <[email protected]> |
345
|
|
|
*/ |
346
|
|
View Code Duplication |
function maxValue($collection, callable $callback) |
347
|
|
|
{ |
348
|
1 |
|
$values = array_map($callback, $collection); |
349
|
1 |
|
$keys = array_flip($values); |
350
|
|
|
|
351
|
1 |
|
return $collection[$keys[max($values)]]; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Merges all arrays to first array |
357
|
|
|
* |
358
|
|
|
* @param array|Traversable $a |
359
|
|
|
* @param array|Traversable $b |
360
|
|
|
* |
361
|
|
|
* @author Aurimas Niekis <[email protected]> |
362
|
|
|
*/ |
363
|
|
|
function merge(&$a, $b) |
|
|
|
|
364
|
|
|
{ |
365
|
1 |
|
$a = call_user_func_array('array_merge', func_get_args()); |
366
|
1 |
|
} |
367
|
|
|
|
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Returns the minimum value in collection using callback method |
371
|
|
|
* |
372
|
|
|
* @param array $collection |
373
|
|
|
* @param callable $callback |
374
|
|
|
* |
375
|
|
|
* @return array |
376
|
|
|
* @author Aurimas Niekis <[email protected]> |
377
|
|
|
*/ |
378
|
|
View Code Duplication |
function minValue($collection, callable $callback) |
379
|
|
|
{ |
380
|
1 |
|
$values = array_map($callback, $collection); |
381
|
1 |
|
$keys = array_flip($values); |
382
|
|
|
|
383
|
1 |
|
return $collection[$keys[min($values)]]; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Convert an array into a list of [key, value] pairs. |
389
|
|
|
* |
390
|
|
|
* @param array $collection |
391
|
|
|
* |
392
|
|
|
* @return array |
393
|
|
|
* @author Aurimas Niekis <[email protected]> |
394
|
|
|
*/ |
395
|
|
|
function pairs($collection) |
396
|
|
|
{ |
397
|
1 |
|
return array_map( |
398
|
|
|
function ($key, $value) { |
399
|
1 |
|
return [$key, $value]; |
400
|
1 |
|
}, |
401
|
1 |
|
array_keys($collection), |
402
|
|
|
$collection |
403
|
1 |
|
); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Split array into two arrays: one whose elements all satisfy callback and one whose elements all do not satisfy |
409
|
|
|
* callback. |
410
|
|
|
* |
411
|
|
|
* @param array $collection |
412
|
|
|
* @param callable $callback |
413
|
|
|
* |
414
|
|
|
* @return array |
415
|
|
|
* @author Aurimas Niekis <[email protected]> |
416
|
|
|
*/ |
417
|
|
|
function partition($collection, callable $callback) |
418
|
|
|
{ |
419
|
1 |
|
$resultA = []; |
420
|
1 |
|
$resultB = []; |
421
|
|
|
|
422
|
1 |
|
foreach ($collection as $key => $value) { |
423
|
1 |
|
if (call_user_func($callback, $value, $key)) { |
424
|
1 |
|
$resultA[$key] = $value; |
425
|
1 |
|
} else { |
426
|
1 |
|
$resultB[$key] = $value; |
427
|
|
|
} |
428
|
1 |
|
} |
429
|
|
|
|
430
|
1 |
|
return [$resultA, $resultB]; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Extract single property from array of arrays |
436
|
|
|
* |
437
|
|
|
* @param array $collection |
438
|
|
|
* @param string $key |
439
|
|
|
* |
440
|
|
|
* @return array |
441
|
|
|
* @author Aurimas Niekis <[email protected]> |
442
|
|
|
*/ |
443
|
|
|
function pluck($collection, $key) |
444
|
|
|
{ |
445
|
6 |
|
return array_map( |
446
|
|
|
function ($item) use ($key) { |
447
|
6 |
|
return get($item, $key); |
448
|
6 |
|
}, |
449
|
|
|
$collection |
450
|
6 |
|
); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Returns the values in array without the elements that the truth test callback passes. The opposite of array_filter. |
455
|
|
|
* |
456
|
|
|
* @param array $collection |
457
|
|
|
* @param callable $callback |
458
|
|
|
* |
459
|
|
|
* @return array |
460
|
|
|
* @author Aurimas Niekis <[email protected]> |
461
|
|
|
*/ |
462
|
|
|
function reject($collection, callable $callback) |
463
|
|
|
{ |
464
|
|
|
return array_filter($collection, function ($item) use ($callback) { |
465
|
1 |
|
return false === call_user_func($callback, $item); |
466
|
1 |
|
}); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Returns the rest of the elements in an array. Pass an from to return the values of the array from that index onward. |
471
|
|
|
* |
472
|
|
|
* @param array $collection |
473
|
|
|
* @param int $from |
474
|
|
|
* |
475
|
|
|
* @return array |
476
|
|
|
* @author Aurimas Niekis <[email protected]> |
477
|
|
|
*/ |
478
|
|
|
function rest($collection, $from = 1) |
479
|
|
|
{ |
480
|
6 |
|
return array_slice($collection, $from); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Returns a reversed array |
485
|
|
|
* |
486
|
|
|
* @param array $collection |
487
|
|
|
* @param boolean $preserveNumericKeys |
488
|
|
|
* |
489
|
|
|
* @return array |
490
|
|
|
* @author Rod Elias <[email protected]> |
491
|
|
|
*/ |
492
|
|
|
function reverse($collection, $preserveNumericKeys = false) |
493
|
|
|
{ |
494
|
1 |
|
return array_reverse($collection, $preserveNumericKeys); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Computes the size of a collection |
499
|
|
|
* |
500
|
|
|
* @param array $collection collection |
501
|
|
|
* @param boolean $countRecursive count or not to count recursively |
502
|
|
|
* |
503
|
|
|
* @return int number of elements in a collection |
504
|
|
|
* @author Rod Elias <[email protected]> |
505
|
|
|
*/ |
506
|
|
|
function size($collection, $countRecursive = false) |
507
|
|
|
{ |
508
|
6 |
|
return \count($collection, $countRecursive); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Returns true if any of the values in the array pass the callback truth test. |
513
|
|
|
* |
514
|
|
|
* @author Aurimas Niekis <[email protected]> |
515
|
|
|
* |
516
|
|
|
* @param array $collection |
517
|
|
|
* @param callable $callback |
518
|
|
|
* |
519
|
|
|
* @return bool |
520
|
|
|
*/ |
521
|
|
|
function some($collection, callable $callback = null) |
522
|
|
|
{ |
523
|
5 |
|
if (null === $callback) { |
524
|
|
|
$callback = function ($item) use ($callback) { |
525
|
3 |
|
return (true == $item); |
526
|
3 |
|
}; |
527
|
3 |
|
} |
528
|
|
|
|
529
|
5 |
|
foreach ($collection as $item) { |
530
|
5 |
|
if (call_user_func($callback, $item)) { |
531
|
4 |
|
return true; |
532
|
|
|
} |
533
|
1 |
|
} |
534
|
|
|
|
535
|
1 |
|
return false; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Returns a sorted array by callback function which should return value to which sort |
541
|
|
|
* |
542
|
|
|
* @param array $collection |
543
|
|
|
* @param callable|string $sortBy |
544
|
|
|
* @param string $sortFunction |
545
|
|
|
* |
546
|
|
|
* @return array |
547
|
|
|
* @author Aurimas Niekis <[email protected]> |
548
|
|
|
*/ |
549
|
|
|
function sortBy($collection, $sortBy, $sortFunction = 'asort') |
550
|
|
|
{ |
551
|
4 |
|
if (false === is_callable($sortBy)) { |
552
|
|
|
$sortBy = function ($item) use ($sortBy) { |
553
|
2 |
|
return $item[$sortBy]; |
554
|
2 |
|
}; |
555
|
2 |
|
} |
556
|
|
|
|
557
|
4 |
|
$values = array_map($sortBy, $collection); |
558
|
4 |
|
$sortFunction($values); |
559
|
|
|
|
560
|
4 |
|
$result = []; |
561
|
4 |
|
foreach ($values as $key => $value) { |
562
|
4 |
|
$result[$key] = $collection[$key]; |
563
|
4 |
|
} |
564
|
|
|
|
565
|
4 |
|
return $result; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* @see collection_rest |
570
|
|
|
* |
571
|
|
|
* @param array $collection |
572
|
|
|
* @param int $from |
573
|
|
|
* |
574
|
|
|
* @return array |
575
|
|
|
* @author Aurimas Niekis <[email protected]> |
576
|
|
|
*/ |
577
|
|
|
function tail($collection, $from = 1) |
578
|
|
|
{ |
579
|
3 |
|
return rest($collection, $from); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Returns the JSON representation of a collection |
584
|
|
|
* @param array $collection |
585
|
|
|
* @return string string containing the JSON representation of $collection |
586
|
|
|
* @author Rod Elias <[email protected]> |
587
|
|
|
*/ |
588
|
|
|
function toJson($collection) |
589
|
|
|
{ |
590
|
1 |
|
return json_encode($collection); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of |
595
|
|
|
* the arrays. |
596
|
|
|
* |
597
|
|
|
* @param array $collectionFirst |
598
|
|
|
* @param array $collectionSecond |
599
|
|
|
* |
600
|
|
|
* @return array |
601
|
|
|
* @author Aurimas Niekis <[email protected]> |
602
|
|
|
*/ |
603
|
|
|
function union($collectionFirst, $collectionSecond) |
|
|
|
|
604
|
|
|
{ |
605
|
2 |
|
$result = call_user_func_array('array_merge', func_get_args()); |
606
|
|
|
|
607
|
2 |
|
return array_unique($result); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* The opposite of zip. Given a number of arrays, returns a series of new arrays, the first of which contains all of |
613
|
|
|
* the first elements in the input arrays, the second of which contains all of the second elements, and so on. |
614
|
|
|
* |
615
|
|
|
* @param array $collection |
616
|
|
|
* |
617
|
|
|
* @return array |
618
|
|
|
* @author Aurimas Niekis <[email protected]> |
619
|
|
|
*/ |
620
|
|
|
function unzip($collection) |
621
|
|
|
{ |
622
|
4 |
|
$length = count(max($collection, 'count')); |
623
|
4 |
|
$result = []; |
624
|
|
|
|
625
|
4 |
|
for ($i = 0; $i < $length; $i++) { |
626
|
4 |
|
$result[$i] = pluck($collection, $i); |
627
|
4 |
|
} |
628
|
|
|
|
629
|
4 |
|
return $result; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* Looks through each value in the array, returning an array of all the values that contain all of the key-value pairs |
635
|
|
|
* listed in properties. |
636
|
|
|
* |
637
|
|
|
* @param array $collection |
638
|
|
|
* @param array $value |
639
|
|
|
* |
640
|
|
|
* @return array |
641
|
|
|
* @author Aurimas Niekis <[email protected]> |
642
|
|
|
*/ |
643
|
|
|
function where($collection, $value) |
644
|
|
|
{ |
645
|
1 |
|
$result = []; |
646
|
|
|
|
647
|
1 |
View Code Duplication |
foreach ($collection as $key => $item) { |
|
|
|
|
648
|
1 |
|
$diff = array_diff_assoc($value, $item); |
649
|
|
|
|
650
|
1 |
|
if (count($diff) < 1) { |
651
|
1 |
|
$result[$key] = $item; |
652
|
1 |
|
} |
653
|
1 |
|
} |
654
|
|
|
|
655
|
1 |
|
return $result; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Returns a copy of the array with all instances of the values removed. |
661
|
|
|
* |
662
|
|
|
* |
663
|
|
|
* @param array $collection |
664
|
|
|
* @param array $without |
665
|
|
|
* |
666
|
|
|
* @return array |
667
|
|
|
* @author Aurimas Niekis <[email protected]> |
668
|
|
|
*/ |
669
|
|
|
function without($collection, $without) |
|
|
|
|
670
|
|
|
{ |
671
|
2 |
|
$without = func_get_args(); |
672
|
2 |
|
array_shift($without); |
673
|
|
|
|
674
|
2 |
|
return array_diff($collection, $without); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Merges together the values of each of the arrays with the values at the corresponding position. |
680
|
|
|
* |
681
|
|
|
* @param array $collectionFirst |
682
|
|
|
* @param array $collectionSecond |
683
|
|
|
* |
684
|
|
|
* @return array |
685
|
|
|
* @author Aurimas Niekis <[email protected]> |
686
|
|
|
*/ |
687
|
|
|
function zip($collectionFirst, $collectionSecond) |
|
|
|
|
688
|
|
|
{ |
689
|
2 |
|
return unzip(func_get_args()); |
690
|
|
|
} |
691
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.