By John Koster
max($key = null)
The max
method can be used to retrieve the maximum value of the given $key
. By default, the $key
is null
and will function in a similar way to PHP's max
function.
The following code example highlights the usage of max
without specifying a $key
:
1<?php
2
3use Illuminate\Support\Collection;
4
5// Create a new collection instance.
6$collection = new Collection([
7 0, 1, 2, 3, 4, 5
8]);
9
10// 5
11$max = $collection->max();
After the above code has executed the $max
variable would contain the value 5
. The following code example shows how to use the max
function when specifying a $key
:
1<?php
2
3use Illuminate\Support\Collection;
4
5// Create a new collection instance.
6$collection = new Collection([
7 ['name' => 'Laravel', 'version' => '5.1'],
8 ['name' => 'Lumen', 'version' => '5.0']
9]);
10
11// 5.1
12$max = $collection->max('version');
13
14// Lumen
15$maxName = $collection->max('name');
After the above code has executed the $max
variable would contain the value 5.1
and the $maxName
variable would contain the value Lumen
.
∎