MeshWorld India LogoMeshWorld.

Create a Laravel Collection from an Array

(Updated: Mar 27, 2026)
Listen to ArticleAI Speech
~5 min read narration
100%
Create a Laravel Collection from an Array

collect() wraps any PHP array in a Collection instance — giving you all of Laravel’s chainable methods without touching the database. It works with plain arrays, API responses, or any structured data you have in memory. You don’t need an Eloquent model. You don’t need a database query. Just pass the array in, and all of Laravel’s collection methods are available immediately.

How do you create a Collection from a plain PHP array?

Two ways. The first is the global collect() helper — the one you’ll use 95% of the time:

PHP
$languages = ['Gujarati', 'Hindi', 'Sanskrit', 'Tamil', 'Urdu'];

$collection = collect($languages);

The second is direct class instantiation. You’ll see this in libraries and packages that avoid global helpers:

PHP
use Illuminate\Support\Collection;

$collection = new Collection(['Gujarati', 'Hindi', 'Sanskrit', 'Tamil', 'Urdu']);

Both produce an identical Illuminate\Support\Collection instance. Pick whichever reads better in context.

The scenario: You hit a third-party REST API and get back a JSON-decoded PHP array of products. You could loop over it with foreach and build the result manually — or you could wrap it in collect() and use filter(), map(), and groupBy() in three lines. The API doesn’t know about Eloquent. That doesn’t matter.

How do you chain methods right after creating a Collection?

You don’t have to assign the collection to a variable first. Chain directly off collect():

PHP
$total = collect([15.99, 9.49, 4.99, 22.00])->sum();
// 52.47

That’s the point. Once you’re in collection-land, you stay there.

What can you do with filter() and map() on an array-backed Collection?

filter() keeps only the items where your callback returns true. Here the prices array is filtered to items over $10, then totalled:

PHP
$prices = [15.99, 9.49, 4.99, 22.00, 7.50, 18.75];

$highValueTotal = collect($prices)
    ->filter(fn($price) => $price > 10)
    ->sum();

// Result: 56.74

map() transforms each item and returns a new Collection. This example pulls just the name from each product array:

PHP
$products = [
    ['name' => 'Keyboard', 'price' => 79.00],
    ['name' => 'Mouse',    'price' => 29.00],
    ['name' => 'Monitor',  'price' => 349.00],
];

$names = collect($products)
    ->map(fn($product) => $product['name']);

// Collection: ['Keyboard', 'Mouse', 'Monitor']

How do you group a plain array with groupBy()?

groupBy() organises items by a key or a callback. This is the method that saves the most boilerplate compared to a manual foreach:

PHP
$orders = [
    ['status' => 'pending',   'amount' => 50],
    ['status' => 'completed', 'amount' => 120],
    ['status' => 'pending',   'amount' => 30],
    ['status' => 'completed', 'amount' => 80],
];

$grouped = collect($orders)->groupBy('status');

// Result:
// 'pending'   => [['status' => 'pending', 'amount' => 50], [...]]
// 'completed' => [['status' => 'completed', 'amount' => 120], [...]]

The scenario: You fetch order data from an external API. It comes back as a flat array. You need to display orders in groups on the checkout summary page. groupBy() on a collect()-wrapped array does this in one line — no need for a DB query, no need for a second Eloquent relationship.

Does collect() work with associative arrays?

Yes. Keys are preserved. Methods like keys(), values(), and get() work as expected:

PHP
$config = [
    'timeout' => 30,
    'retries' => 3,
    'base_url' => 'https://api.example.com',
];

$timeout = collect($config)->get('timeout'); // 30
$keys    = collect($config)->keys()->all();  // ['timeout', 'retries', 'base_url']

Summary

  • collect($array) and new Collection($array) are equivalent. Use whichever fits your style.
  • You can chain filter(), map(), sum(), groupBy(), and any other collection method immediately after wrapping.
  • Array-backed collections are ideal for API responses and in-memory data that doesn’t come from Eloquent.
  • Key order is preserved. Associative arrays work without any extra configuration.
  • No database connection is required — these are pure PHP operations.

FAQ

Is there a performance difference between collect() and new Collection()? None. collect() is literally a global helper that calls new Collection() internally. The compiled result is identical.

Can I convert a Collection back to a plain array? Yes. Call ->all() to get the raw array, or ->toArray() for a recursive conversion (useful for nested collections).

Does collect() work with objects? Yes. You can pass an array of objects or even a single object (it’ll be treated as an array with one item). Methods like pluck() work on object properties as well as array keys.

Can I create an empty Collection? collect() with no argument (or collect([])) returns an empty Collection. You can push items in later with ->push() or ->put().

Does collect() load the array into memory all at once? Yes. Unlike lazy collections (LazyCollection), a standard Collection holds all items in memory. For very large datasets, consider LazyCollection::make() instead.

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive