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.
1public function zip(2 $items3);
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, 612]);
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 410 1 =>11 object(Illuminate\Support\Collection)12 protected 'items' =>13 array14 0 => int 215 1 => int 516 2 =>17 object(Illuminate\Support\Collection)18 protected 'items' =>19 array20 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:
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, 912]);13 14// Notice the call to all() at the end of this15// method chain to return the array of16// zipped results17$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 1010 4 => int 1211 1 =>12 object(Illuminate\Support\Collection)13 protected 'items' =>14 array15 0 => int 216 1 => int 517 2 => int 818 3 => int 1119 4 => int 1320 2 =>21 object(Illuminate\Support\Collection)22 protected 'items' =>23 array24 0 => int 325 1 => int 626 2 => int 927 3 => null28 4 => int 1429 3 =>30 object(Illuminate\Support\Collection)31 protected 'items' =>32 array33 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.