Laravel Collection Public API: sum

November 30, 2016 —John Koster

sum($callback = null)

The sum is a simple method that returns the sum of all items in a collection. It also defines a $callback parameter which can be used to tell the sum method which values of the collection should be added together. If no items are in the collection, the sum method will return 0.

The following code example shows the simplest way to use the sum method:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 1, 2, 3, 4, 5
8]);
9 
10// 15
11$sum = $collection->sum();

After the above code has executed, the $sum variable will contain the value 15, which is indeed the sum of all the items in the collection.

The following is a more interesting example, and shows the total number of speakers at Laracon EU for the years 2013, 2014 and 2015. The number of speakers is not unique, and therefore not technically accurate:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 ['year' => '2015', 'speakers' => 12],
8 ['year' => '2014', 'speakers' => 21],
9 ['year' => '2013', 'speakers' => 10]
10 
11]);
12 
13// 43
14$totalSpeakers = $collection->sum('speakers');

After the above code has executed, the $totalSpeakers variable would contain the value 43.

The following collection also contains the number of speakers that spoke Laracon EU from 2013 to 2015, but instead of representing the speakers as number, the collection contains lists the names of the speakers.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 [
8 'year' => '2015',
9 'speakers' => [
10 'Jeffrey',
11 'Taylor',
12 'Matt',
13 'Konstantin',
14 'Jessica',
15 'Frank',
16 'Adam',
17 'Lorna',
18 'Dries',
19 'Ben',
20 'Esther',
21 'Hannes'
22 ]
23 ],
24 [
25 'year' => '2014',
26 'speakers' => [
27 'Taylor',
28 'Ross',
29 'Erika',
30 'Konstantin',
31 'Andreas',
32 'Kayla',
33 'Matthias',
34 'Mathias',
35 'Ben',
36 'Rafael',
37 'Igor',
38 'Michelle',
39 'Adam',
40 'Xander',
41 'Wim',
42 'Mitchell',
43 'Gabriela',
44 'Matt',
45 'Frank',
46 'Hannes',
47 'Kirk'
48 ]
49 ],
50 [
51 'year' => '2013',
52 'speakers' => [
53 'Taylor',
54 'Fabien',
55 'Phill',
56 'Jordi',
57 'Kapil',
58 'Matthew',
59 'Frank',
60 'Jeffrey',
61 'Ben',
62 'Ross'
63 ]
64 ]
65]);

The total number of speakers can be counted using the sum method by passing a function as the argument to the $callback parameter. The callback function passed must define one parameter, which will be the value of the item. The following code example shows how to get the sum of all the speakers in the above collection:

1<?php
2 
3// 43
4$totalSpeakers = $collection->sum(function($value) {
5 return count($value['speakers']);
6});

After the above code has executed, the value of $totalSpeakers will be 43, just as it was in the previous example. It is important to note that the callback function should return a numerical value, and in the above example it is returning the result of the count function.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.