Laravel 5 Collections: Pair the Values of a Collection With the Values of Other Collections With zip

April 22, 2018 —John Koster

The zip method is used to merge the values of two collections. The first collection will be the collection that the zip method was invoked on, the second collection is supplied as an argument. The zip method produces results that are similar to Python's zip function, and developer's coming from a Python background will recognize the behavior of the zip method.

The newly created collection will have its elements placed at the corresponding index from the first collection.

The zip method returns a new Collection instance and does not modify the original collection in any way.

#Signature

1public function zip(
2 $items
3);

#Example Use

The following example demonstrates the basic use of the zip function:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 1, 2, 3
6]);
7 
8// Create a new collection by zipping the original
9// collection with a new array of values.
10$zipped = $collection->zip([
11 4, 5, 6
12]);

After the above code has executed, the $zipped variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 =>
5 object(Illuminate\Support\Collection)
6 protected 'items' =>
7 array
8 0 => int 1
9 1 => int 4
10 1 =>
11 object(Illuminate\Support\Collection)
12 protected 'items' =>
13 array
14 0 => int 2
15 1 => int 5
16 2 =>
17 object(Illuminate\Support\Collection)
18 protected 'items' =>
19 array
20 0 => int 3
21 1 => int 6

The zip method can accept multiple arguments, and they can be any data type that can be converted to an array:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 1, 2, 3
6]);
7 
8// Create a new collection instance to use an
9// an argument for the zip method.
10$testCollection = new Collection([
11 7, 8, 9
12]);
13 
14// Notice the call to all() at the end of this
15// method chain to return the array of
16// zipped results
17$zippedResults = $collection->zip(
18 [4, 5, 6],
19 $testCollection,
20 [10, 11],
21 [12, 13, 14, 15]
22)->all();

After the above code has executed, the $zippedResults variable would contain the following value:

1array (size=4)
2 0 =>
3 object(Illuminate\Support\Collection)
4 protected 'items' =>
5 array
6 0 => int 1
7 1 => int 4
8 2 => int 7
9 3 => int 10
10 4 => int 12
11 1 =>
12 object(Illuminate\Support\Collection)
13 protected 'items' =>
14 array
15 0 => int 2
16 1 => int 5
17 2 => int 8
18 3 => int 11
19 4 => int 13
20 2 =>
21 object(Illuminate\Support\Collection)
22 protected 'items' =>
23 array
24 0 => int 3
25 1 => int 6
26 2 => int 9
27 3 => null
28 4 => int 14
29 3 =>
30 object(Illuminate\Support\Collection)
31 protected 'items' =>
32 array
33 0 => null
34 1 => null
35 2 => null
36 3 => null
37 4 => int 15

There are a few important things to notice in the above output. The [10, 11] array that was passed as an argument had less items than the other arrays when building the final result. As a result, the value null was used for the fourth item in the third Collection instance The other important thing to notice is a result of the [12, 13, 14, 15] argument having more items that the other arrays.

Because the [12, 13, 14, 15] argument had more items, a new fourth Collection was created as part of the zipped results. All values were set to null, except for the fifth value, which was assigned the value 15.

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.