Skip to content

Commit 7a7678b

Browse files
authored
Document Collection::median() and Collection::mode() methods
Documents the features added by laravel/framework#14305 I also changed the Collection::avg() documentation with the same code snippet to better demonstrate where they differ since they all are related. Since they can be confusing to understand, I also added reference links to Wikipedia to each one.
1 parent 6cf0ac5 commit 7a7678b

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)
@@ -147,22 +149,15 @@ The `all` method returns the underlying array represented by the collection:
147149
<a name="method-avg"></a>
148150
#### `avg()` {#collection-method}
149151

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

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

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

163-
$collection->avg('pages');
158+
$average = collect([1, 1, 2, 4])->avg();
164159

165-
// 636
160+
// 2
166161

167162
<a name="method-chunk"></a>
168163
#### `chunk()` {#collection-method}
@@ -737,6 +732,19 @@ The `max` method returns the maximum value of a given key:
737732

738733
// 5
739734

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

@@ -773,6 +781,19 @@ The `min` method returns the minimum value of a given key:
773781

774782
// 1
775783

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

0 commit comments

Comments
 (0)