Search

Laravel Collection Public API: zip

November 30, 2016 —John Koster

zip($items)

The zip method is used to merge the values of the $items array with the values within the Collection at the corresponding index. 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 zip method returns a new Collection instance and does not modify the original collection in any way.

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

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)[135]
2 protected 'items' =>
3 array (size=3)
4 0 =>
5 object(Illuminate\Support\Collection)[136]
6 protected 'items' =>
7 array (size=2)
8 0 => int 1
9 1 => int 4
10 1 =>
11 object(Illuminate\Support\Collection)[137]
12 protected 'items' =>
13 array (size=2)
14 0 => int 2
15 1 => int 5
16 2 =>
17 object(Illuminate\Support\Collection)[138]
18 protected 'items' =>
19 array (size=2)
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:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 1, 2, 3
8]);
9 
10// Create a new collection instance to use an
11// an argument for the zip method.
12$testCollection = new Collection([
13 7, 8, 9
14]);
15 
16// Notice the call to all() at the end of this
17// method chain to return the array of
18// zipped results
19$zippedResults = $collection->zip(
20 [4, 5, 6],
21 $testCollection,
22 [10, 11],
23 [12, 13, 14, 15]
24)->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)[137]
4 protected 'items' =>
5 array (size=5)
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)[138]
13 protected 'items' =>
14 array (size=5)
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)[139]
22 protected 'items' =>
23 array (size=5)
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)[140]
31 protected 'items' =>
32 array (size=5)
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.