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 original11// collection with a new array of values.12$zipped = $collection->zip([13 4, 5, 614]);
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 410 1 =>11 object(Illuminate\Support\Collection)[137]12 protected 'items' =>13 array (size=2)14 0 => int 215 1 => int 516 2 =>17 object(Illuminate\Support\Collection)[138]18 protected 'items' =>19 array (size=2)20 0 => int 321 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 an11// an argument for the zip method.12$testCollection = new Collection([13 7, 8, 914]);15 16// Notice the call to all() at the end of this17// method chain to return the array of18// zipped results19$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 1010 4 => int 1211 1 =>12 object(Illuminate\Support\Collection)[138]13 protected 'items' =>14 array (size=5)15 0 => int 216 1 => int 517 2 => int 818 3 => int 1119 4 => int 1320 2 =>21 object(Illuminate\Support\Collection)[139]22 protected 'items' =>23 array (size=5)24 0 => int 325 1 => int 626 2 => int 927 3 => null28 4 => int 1429 3 =>30 object(Illuminate\Support\Collection)[140]31 protected 'items' =>32 array (size=5)33 0 => null34 1 => null35 2 => null36 3 => null37 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
.
∎
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.