Laravel Collections

Combine Multiple Arrays by Index with Collection::zip()

Punyapal Shah 1 min read
edit this tip
Use zip() on Laravel Collections to merge array elements sharing matching index positions into paired sub-collections.

When processing paired tabular data (such as headers and corresponding CSV row values, or parallel arrays of names and scores), indexing arrays manually with $names[$i] requires bounds checking.

zip() pairs corresponding items from multiple arrays together.

Basic Usage

$names = collect(['Punyapal', 'Developer 2', 'Developer 3']);
$scores = [95, 88, 92];

$zipped = $names->zip($scores);

// Output:
// [
//     ['Punyapal', 95],
//     ['Developer 2', 88],
//     ['Developer 3', 92],
// ]

Zipping Multiple Datasets

$users = collect(['User 1', 'User 2']);
$roles = ['Admin', 'Editor'];
$status = ['Active', 'Pending'];

$matrix = $users->zip($roles, $status);
// Output: [['User 1', 'Admin', 'Active'], ['User 2', 'Editor', 'Pending']]

Summary

  • Merges elements of matching index positions across multiple arrays.
  • Fills missing values with null if array lengths differ.
  • Ideal for data matrix transformations and CSV row construction.
Tags: Laravel Collections Data Transformation
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips