Skip to content

Commit befea2f

Browse files
authored
Merge pull request #3223 from paulofreitas/collection-median-and-mode
[5.4] Document Collection::median() and Collection::mode() methods
2 parents 543dfbe + 7a7678b commit befea2f

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

collections.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ For the remainder of this documentation, we'll discuss each method available on
7878
[map](#method-map)
7979
[mapWithKeys](#method-mapwithkeys)
8080
[max](#method-max)
81+
[median](#method-median)
8182
[merge](#method-merge)
8283
[min](#method-min)
84+
[mode](#method-mode)
8385
[nth](#method-nth)
8486
[only](#method-only)
8587
[partition](#method-partition)
@@ -149,22 +151,15 @@ The `all` method returns the underlying array represented by the collection:
149151
<a name="method-avg"></a>
150152
#### `avg()` {#collection-method}
151153

152-
The `avg` method returns the average of all items in the collection:
154+
The `avg` method returns the [average value](https://en.wikipedia.org/wiki/Average) of a given key:
153155

154-
collect([1, 2, 3, 4, 5])->avg();
156+
$average = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->avg('foo');
155157

156-
// 3
157-
158-
If the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:
159-
160-
$collection = collect([
161-
['name' => 'JavaScript: The Good Parts', 'pages' => 176],
162-
['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096],
163-
]);
158+
// 20
164159

165-
$collection->avg('pages');
160+
$average = collect([1, 1, 2, 4])->avg();
166161

167-
// 636
162+
// 2
168163

169164
<a name="method-chunk"></a>
170165
#### `chunk()` {#collection-method}
@@ -739,6 +734,19 @@ The `max` method returns the maximum value of a given key:
739734

740735
// 5
741736

737+
<a name="method-median"></a>
738+
#### `median()` {#collection-method}
739+
740+
The `median` method returns the [median value](https://en.wikipedia.org/wiki/Median) of a given key:
741+
742+
$median = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->median('foo');
743+
744+
// 15
745+
746+
$median = collect([1, 1, 2, 4])->median();
747+
748+
// 1.5
749+
742750
<a name="method-merge"></a>
743751
#### `merge()` {#collection-method}
744752

@@ -775,6 +783,19 @@ The `min` method returns the minimum value of a given key:
775783

776784
// 1
777785

786+
<a name="method-mode"></a>
787+
#### `mode()` {#collection-method}
788+
789+
The `mode` method returns the [mode value](https://en.wikipedia.org/wiki/Mode_(statistics)) of a given key:
790+
791+
$mode = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->mode('foo');
792+
793+
// [10]
794+
795+
$mode = collect([1, 1, 2, 4])->mode();
796+
797+
// [1]
798+
778799
<a name="method-nth"></a>
779800
#### `nth()` {#collection-method}
780801

0 commit comments

Comments
 (0)