Collection::implode

Joins collection items into a string, optionally plucking a key from each item first.

Signature
Collection::implode(callable|string|null $value, ?string $glue = null): string

Parameters

ParameterTypeDescription
$value(callable(TValue, TKey): mixed)|string|nullOn a collection of arrays or objects, the key to pluck from each item, or a callback that formats each item. On a collection of scalars this argument is used as the glue instead.
$glueoptionalstring|null= nullThe separator placed between values. Ignored when the collection holds scalars, because the first argument then serves as the glue. Null is treated as an empty string.

Return value

stringA single string made by joining the items, or the plucked/mapped values, with the glue between them. An empty collection returns an empty string.

implode turns a collection into a string. Its two-argument form does the job that usually takes a pluck followed by PHP's implode: name the key you want from each row, then the separator.

Its arguments change meaning with the data

The method inspects the first item to decide what you meant. If that item is an array or a non-Stringable object, the first argument is treated as a key to pluck and the second as the glue. If it is a scalar or a Stringable, the first argument is the glue and the second is ignored entirely. A callback as the first argument always means "format each item", with the second argument as the glue.

The thing that trips people up

That inspection is why collect([1, 2, 3])->implode('product', ', ') returns 1product2product3 rather than raising an error. The collection holds scalars, so 'product' became the separator and ', ' was silently dropped. Any code path where a collection is sometimes rows and sometimes scalars can produce this without warning.

The decision is made from the first item alone, so a mixed collection is joined according to whatever happens to be at the front. An empty collection always returns ''. For a natural-language list — "a, b and c" — use join, which adds a separate final glue.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Build a comma-separated list of names from a collection of records for an email subject.
  • Turn a row of values into a single CSV line.
  • Format each item with a callback and join the results in one call.

Frequently asked questions

Why is my glue string appearing as the wrong separator?

Because the collection holds scalars, in which case implode treats the first argument as the glue and ignores the second. collect([1,2,3])->implode('product', ', ') returns 1product2product3 — pass only the glue for scalar collections.

How does implode decide whether to pluck a key?

It inspects the first item. Arrays and non-Stringable objects trigger the pluck branch; scalars and Stringable instances do not. A mixed collection is therefore joined according to whatever sits at the front.

How do I get "a, b and c" instead of "a, b, c"?

Use join(), which takes a second glue applied only before the final element. implode has no concept of a final separator.

More examples

Objects and Stringables behave differently

The output is "ada & grace", then "A1 | B2", then string(0) "". Stringable is explicitly excluded from the record branch, which is what lets a collection of Str objects join like plain strings. The empty case never errors, so implode is safe to call on a possibly-empty result without guarding it.

Example · php
<?php
use Illuminate\Support\Str;

// Stringable items count as scalars, so one argument is the glue.
$names = collect([Str::of('ada'), Str::of('grace')]);
echo $names->implode(' & '), PHP_EOL;

// Plain objects are treated as records, so the key is plucked.
$objs = collect([(object) ['sku' => 'A1'], (object) ['sku' => 'B2']]);
echo $objs->implode('sku', ' | '), PHP_EOL;

// An empty collection is always an empty string.
var_dump(collect([])->implode('product', ', '));

Building a CSV line

The loop prints "A1,Desk,120.5" and "B2,Chair,80" — each row is wrapped in its own collection of scalars, so the single argument is the glue. The final line prints "[email protected]; [email protected]", showing the callback form doing formatting and joining together without an intermediate map.

Example · php
<?php
$rows = collect([
    ['sku' => 'A1', 'name' => 'Desk',  'price' => 120.5],
    ['sku' => 'B2', 'name' => 'Chair', 'price' => 80],
]);

foreach ($rows as $row) {
    echo collect($row)->implode(','), PHP_EOL;
}

// Or format each record into one field first.
echo $rows->implode(fn (array $r) => $r['sku'].'@'.number_format($r['price'], 2), '; '), PHP_EOL;

Joining a column from Eloquent models

Both lines produce the same string. The first relies on implode's record branch, since models are non-Stringable objects; the second is more explicit and survives the collection turning out to be empty or scalar. Prefer the pluck form in shared code, where you cannot be sure what the collection holds. This snippet needs a booted application with Eloquent, so it is shown rather than executed.

Example · php
<?php
$subject = 'Assigned to '.$task->assignees->implode('name', ', ');

// Equivalent, and clearer when more shaping is needed:
$subject = 'Assigned to '.$task->assignees->pluck('name')->implode(', ');

Discussion

  • Be the first to comment on this lesson.