You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:
151
-
152
-
$collection = collect([
153
-
['name' => 'JavaScript: The Good Parts', 'pages' => 176],
154
-
['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096],
155
-
]);
160
+
// 20
156
161
157
-
$collection->avg('pages');
162
+
$average = collect([1, 1, 2, 4])->avg();
158
163
159
-
// 636
164
+
// 2
160
165
161
166
<aname="method-chunk"></a>
162
167
#### `chunk()` {#collection-method}
@@ -243,6 +248,13 @@ Finally, you may also pass a callback to the `contains` method to perform your o
243
248
244
249
// false
245
250
251
+
The `contains` method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the [`containsStrict`](#method-containsstrict) method to filter using "strict" comparisons.
252
+
253
+
<aname="method-containsstrict"></a>
254
+
#### `containsStrict()` {#collection-method}
255
+
256
+
This method has the same signature as the [`contains`](#method-contains) method; however, all values are compared using "strict" comparisons.
257
+
246
258
<aname="method-count"></a>
247
259
#### `count()` {#collection-method}
248
260
@@ -606,6 +618,15 @@ The `isEmpty` method returns `true` if the collection is empty; otherwise, `fals
606
618
607
619
// true
608
620
621
+
<aname="method-isnotempty"></a>
622
+
#### `isNotEmpty()` {#collection-method}
623
+
624
+
The `isNotEmpty` method returns `true` if the collection is not empty; otherwise, `false` is returned:
625
+
626
+
collect([])->isNotEmpty();
627
+
628
+
// false
629
+
609
630
<aname="method-keyby"></a>
610
631
#### `keyBy()` {#collection-method}
611
632
@@ -642,7 +663,6 @@ You may also pass a callback to the method. The callback should return the value
642
663
]
643
664
*/
644
665
645
-
646
666
<aname="method-keys"></a>
647
667
#### `keys()` {#collection-method}
648
668
@@ -737,10 +757,23 @@ The `max` method returns the maximum value of a given key:
737
757
738
758
// 5
739
759
760
+
<aname="method-median"></a>
761
+
#### `median()` {#collection-method}
762
+
763
+
The `median` method returns the [median value](https://en.wikipedia.org/wiki/Median) of a given key:
The `merge` method merges the given array into the original collection. If a string key in the given array matches a string key in the original collection, the given array's value will overwrite the value in the original collection:
776
+
The `merge` method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection:
@@ -855,13 +912,13 @@ The `prepend` method adds an item to the beginning of the collection:
855
912
856
913
You may also pass a second argument to set the key of the prepended item:
857
914
858
-
$collection = collect(['one' => 1, 'two', => 2]);
915
+
$collection = collect(['one' => 1, 'two' => 2]);
859
916
860
917
$collection->prepend(0, 'zero');
861
918
862
919
$collection->all();
863
920
864
-
// ['zero' => 0, 'one' => 1, 'two', => 2]
921
+
// ['zero' => 0, 'one' => 1, 'two' => 2]
865
922
866
923
<aname="method-pull"></a>
867
924
#### `pull()` {#collection-method}
@@ -985,7 +1042,7 @@ The `search` method searches the collection for the given value and returns its
985
1042
986
1043
// 1
987
1044
988
-
The search is done using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use strict comparison, pass `true` as the second argument to the method:
1045
+
The search is done using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass `true` as the second argument to the method:
989
1046
990
1047
$collection->search('4', true);
991
1048
@@ -1025,7 +1082,7 @@ The `shuffle` method randomly shuffles the items in the collection:
1025
1082
1026
1083
$shuffled->all();
1027
1084
1028
-
// [3, 2, 5, 1, 4] // (generated randomly)
1085
+
// [3, 2, 5, 1, 4] - (generated randomly)
1029
1086
1030
1087
<aname="method-slice"></a>
1031
1088
#### `slice()` {#collection-method}
@@ -1048,7 +1105,7 @@ If you would like to limit the size of the returned slice, pass the desired size
1048
1105
1049
1106
// [5, 6]
1050
1107
1051
-
The returned slice will preserve keys by default. If you do not wish to preserve the original keys, you can use the `values` method to reindex them.
1108
+
The returned slice will preserve keys by default. If you do not wish to preserve the original keys, you can use the [`values`](#method-values) method to reindex them.
1052
1109
1053
1110
<aname="method-sort"></a>
1054
1111
#### `sort()` {#collection-method}
@@ -1252,7 +1309,7 @@ The `toArray` method converts the collection into a plain PHP `array`. If the co
1252
1309
<aname="method-tojson"></a>
1253
1310
#### `toJson()` {#collection-method}
1254
1311
1255
-
The `toJson` method converts the collection into JSON:
1312
+
The `toJson` method converts the collection into a JSON serialized string:
@@ -1288,7 +1345,7 @@ The `union` method adds the given array to the collection. If the given array co
1288
1345
1289
1346
$union->all();
1290
1347
1291
-
// [1 => ['a'], 2 => ['b'], [3 => ['c']]
1348
+
// [1 => ['a'], 2 => ['b'], 3 => ['c']]
1292
1349
1293
1350
<aname="method-unique"></a>
1294
1351
#### `unique()` {#collection-method}
@@ -1341,6 +1398,13 @@ You may also pass your own callback to determine item uniqueness:
1341
1398
]
1342
1399
*/
1343
1400
1401
+
The `unique` method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the [`uniqueStrict`](#method-uniquestrict) method to filter using "strict" comparisons.
1402
+
1403
+
<aname="method-uniquestrict"></a>
1404
+
#### `uniqueStrict()` {#collection-method}
1405
+
1406
+
This method has the same signature as the [`unique`](#method-unique) method; however, all values are compared using "strict" comparisons.
1407
+
1344
1408
<aname="method-values"></a>
1345
1409
#### `values()` {#collection-method}
1346
1410
@@ -1378,13 +1442,13 @@ The `where` method filters the collection by a given key / value pair:
1378
1442
$filtered->all();
1379
1443
1380
1444
/*
1381
-
[
1382
-
['product' => 'Chair', 'price' => 100],
1383
-
['product' => 'Door', 'price' => 100],
1384
-
]
1445
+
[
1446
+
['product' => 'Chair', 'price' => 100],
1447
+
['product' => 'Door', 'price' => 100],
1448
+
]
1385
1449
*/
1386
1450
1387
-
The `where` method uses loose comparisons when checking item values. Use the [`whereStrict`](#method-wherestrict) method to filter using "strict" comparisons.
1451
+
The `where` method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the [`whereStrict`](#method-wherestrict) method to filter using "strict" comparisons.
1388
1452
1389
1453
<aname="method-wherestrict"></a>
1390
1454
#### `whereStrict()` {#collection-method}
@@ -1394,7 +1458,7 @@ This method has the same signature as the [`where`](#method-where) method; howev
1394
1458
<aname="method-wherein"></a>
1395
1459
#### `whereIn()` {#collection-method}
1396
1460
1397
-
The `whereIn` method filters the collection by a given key / value contained within the given array.
1461
+
The `whereIn` method filters the collection by a given key / value contained within the given array:
1398
1462
1399
1463
$collection = collect([
1400
1464
['product' => 'Desk', 'price' => 200],
@@ -1408,18 +1472,18 @@ The `whereIn` method filters the collection by a given key / value contained wit
1408
1472
$filtered->all();
1409
1473
1410
1474
/*
1411
-
[
1412
-
['product' => 'Bookcase', 'price' => 150],
1413
-
['product' => 'Desk', 'price' => 200],
1414
-
]
1475
+
[
1476
+
['product' => 'Bookcase', 'price' => 150],
1477
+
['product' => 'Desk', 'price' => 200],
1478
+
]
1415
1479
*/
1416
1480
1417
-
The `whereIn` method uses "loose" comparisons when checking item values. Use the [`whereInStrict`](#method-whereinstrict) method to filter using strict comparisons.
1481
+
The `whereIn` method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the [`whereInStrict`](#method-whereinstrict) method to filter using "strict" comparisons.
1418
1482
1419
1483
<aname="method-whereinstrict"></a>
1420
1484
#### `whereInStrict()` {#collection-method}
1421
1485
1422
-
This method has the same signature as the [`whereIn`](#method-wherein) method; however, all values are compared using strict comparisons.
1486
+
This method has the same signature as the [`whereIn`](#method-wherein) method; however, all values are compared using "strict" comparisons.
0 commit comments