Fluent, chainable wrapper around arrays. Namespace Zero\Lib\Support\Collection (aliased as Collection). Build with the global collect() helper or Collection::make().
use Zero\Lib\Support\Collection;
collect([1, 2, 3, 4, 5])
->filter(fn ($v) => $v > 2)
->map(fn ($v) => $v * 10)
->values()
->all(); // [30, 40, 50]Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, so count($c), foreach ($c as ...), $c[$key], and json_encode($c) work directly.
Topics: Building · Conversion · Iteration · Filtering · Querying · Mutation · Slicing · Reshaping · Set Operations · Sorting · Aggregates · Conditional
Static factories on Collection. Implementation: top of Collection.php.
collect([1, 2, 3])->count(); // 3
Collection::make(['a' => 1])->all(); // ['a' => 1]Wrap any value.
Collection::wrap('a')->all(); // ['a']
Collection::wrap(null)->all(); // []Collection::range(1, 3)->all(); // [1, 2, 3]Collection::times(3, fn ($i) => $i * 2)->all(); // [2, 4, 6]Implementation: Concerns/Collection/Conversion.php.
collect([1, 2, 3])->all(); // [1, 2, 3]Recursively convert nested Collections / JsonSerializable.
collect([collect([1, 2])])->toArray(); // [[1, 2]]collect([1, 2])->toJson(); // '[1,2]'
json_encode(collect([1, 2])); // '[1,2]'collect([1, 2])->count(); // 2
collect([])->isEmpty(); // true
collect([1])->isNotEmpty(); // true
count(collect([1, 2, 3])); // 3collect(['a' => 1, 'b' => 2])->keys()->all(); // ['a', 'b']
collect(['a' => 1, 'b' => 2])->values()->all(); // [1, 2]$c = collect(['a' => 1]);
$c['a']; // 1
foreach ($c as $k => $v) { /* ... */ }Implementation: Concerns/Collection/Iteration.php.
collect([1, 2, 3])->map(fn ($v) => $v * 2)->all(); // [2, 4, 6]collect([['id' => 1, 'n' => 'a']])
->mapWithKeys(fn ($v) => [$v['id'] => $v['n']])
->all(); // [1 => 'a']Spread each item's values as positional arguments.
collect([[1, 2], [3, 4]])
->mapSpread(fn ($a, $b) => $a + $b)
->all(); // [3, 7]Map then collapse.
collect([[1, 2], [3]])->flatMap(fn ($v) => $v)->all(); // [1, 2, 3]Iterate; return false from the callback to stop.
collect([1, 2, 3])->each(function ($v) { /* side effect */ });collect([[1, 2], [3, 4]])->eachSpread(fn ($a, $b) => /* ... */);collect([1, 2, 3])->reduce(fn ($carry, $v) => $carry + $v, 0); // 6collect([['id' => 1], ['id' => 2]])->pluck('id')->all(); // [1, 2]Returns a Collection of two Collections.
[$evens, $odds] = collect([1, 2, 3, 4])->partition(fn ($v) => $v % 2 === 0);
$evens->all(); // [1 => 2, 3 => 4]
$odds->all(); // [0 => 1, 2 => 3]collect([['id' => 1], ['id' => 2]])->keyBy('id')->keys()->all(); // [1, 2]Each group is itself a Collection.
collect([['t' => 'a'], ['t' => 'a'], ['t' => 'b']])
->groupBy('t')
->count(); // 2Implementation: Concerns/Collection/Filtering.php.
Without a callback, drops falsy values.
collect([0, 1, 2])->filter()->values()->all(); // [1, 2]
collect([1, 2, 3, 4])->filter(fn ($v) => $v > 2)->values()->all(); // [3, 4]Inverse of filter.
collect([1, 2, 3, 4])->reject(fn ($v) => $v % 2 === 0)->values()->all(); // [1, 3]Two-argument form is =. Operators: =, ==, ===, !=, !==, <>, >, >=, <, <=.
$users = [['active' => true], ['active' => false]];
collect($users)->where('active', true)->count(); // 1
collect([['n' => 5], ['n' => 1]])->where('n', '>', 3)->count(); // 1collect([['t' => 'a'], ['t' => 'b'], ['t' => 'c']])
->whereIn('t', ['a', 'c'])
->count(); // 2collect([1, null, 2])->whereNotNull()->values()->all(); // [1, 2]
collect([['n' => 1], ['n' => null]])->whereNull('n')->count(); // 1collect(['a' => 1, 'b' => 2, 'c' => 3])->only(['a', 'c'])->all(); // ['a' => 1, 'c' => 3]Scalar / closure / where form.
collect([1, 2, 3])->contains(2); // true
collect([1, 2, 3])->contains(fn ($v) => $v > 2); // true
collect([['t' => 'a']])->contains('t', 'a'); // trueReturns true only when every key exists.
collect(['a' => 1])->has('a'); // true
collect(['a' => 1])->has(['a', 'b']); // falseImplementation: Concerns/Collection/Querying.php.
collect([1, 2, 3])->first(); // 1
collect([1, 2, 3])->first(fn ($v) => $v > 1); // 2
collect([1, 2, 3])->last(); // 3collect(['a' => 1])->get('a'); // 1
collect([])->get('x', 'fallback'); // 'fallback'Returns the matching key (or false). Accepts a closure.
collect(['a', 'b', 'c'])->search('b'); // 1
collect([1, 2, 3])->search(fn ($v) => $v === 2); // 1collect([1, 2, 3, 4, 5])->random(); // single item
collect([1, 2, 3, 4, 5])->random(3); // Collection of 3 itemsThese mutate the underlying items in place and return $this. Implementation: Concerns/Collection/Mutation.php.
collect()->put('a', 1)->all(); // ['a' => 1]Read and remove.
$c = collect(['a' => 1, 'b' => 2]);
$c->pull('a'); // 1; $c->all() === ['b' => 2]collect([1])->push(2, 3)->all(); // [1, 2, 3]collect([2, 3])->prepend(1)->all(); // [1, 2, 3]
collect(['b' => 2])->prepend(1, 'a')->all(); // ['a' => 1, 'b' => 2]With $count = 1 returns the item; otherwise returns a Collection.
collect([1, 2, 3])->pop(); // 3
collect([1, 2, 3])->shift(); // 1collect(['a' => 1, 'b' => 2])->forget('a')->all(); // ['b' => 2]Implementation: Concerns/Collection/Slicing.php.
collect([1, 2, 3, 4])->take(2)->all(); // [1, 2]
collect([1, 2, 3, 4])->take(-2)->all(); // [3, 4]collect([1, 2, 3, 4])->skip(2)->values()->all(); // [3, 4]collect([1, 2, 3, 4])->slice(1, 2)->values()->all(); // [2, 3]Returns a Collection of Collections.
collect([1, 2, 3, 4, 5])->chunk(2)->count(); // 3collect([1, 2, 3, 4, 5, 6])->nth(2)->all(); // [1, 3, 5]
collect([1, 2, 3, 4, 5, 6])->nth(2, 1)->all(); // [2, 4, 6]Implementation: Concerns/Collection/Reshaping.php.
Merge a list of arrays.
collect([[1, 2], [3]])->collapse()->all(); // [1, 2, 3]collect([1, [2, [3]]])->flatten()->all(); // [1, 2, 3]collect(['a', 'b'])->flip()->all(); // ['a' => 0, 'b' => 1]collect(['a' => ['b' => 1]])->dot()->all(); // ['a.b' => 1]
collect(['a.b' => 1])->undot()->all(); // ['a' => ['b' => 1]]Preserves keys.
collect([1, 2, 3])->reverse()->values()->all(); // [3, 2, 1]collect([1, 2])->zip([3, 4])->first()->all(); // [1, 3]Implementation: Concerns/Collection/SetOperations.php.
collect([1, 2])->merge([3, 4])->all(); // [1, 2, 3, 4]Append numerically (no key collision).
collect([1, 2])->concat([3])->all(); // [1, 2, 3]Use this collection's values as keys, combine with $values.
collect(['a', 'b'])->combine([1, 2])->all(); // ['a' => 1, 'b' => 2]collect([1, 2, 3])->diff([2])->values()->all(); // [1, 3]collect([1, 2, 3])->intersect([2, 3, 4])->values()->all(); // [2, 3]Implementation: Concerns/Collection/Sorting.php.
collect([3, 1, 2])->sort()->values()->all(); // [1, 2, 3]collect([['n' => 3], ['n' => 1]])
->sortBy('n')
->values()
->pluck('n')
->all(); // [1, 3]collect([['n' => 1], ['n' => 3]])->sortByDesc('n')->values()->pluck('n')->all(); // [3, 1]Without a key/callback.
collect([1, 3, 2])->sortDesc()->values()->all(); // [3, 2, 1]collect(['b' => 1, 'a' => 2])->sortKeys()->keys()->all(); // ['a', 'b']collect([1, 2, 2, 3])->unique()->values()->all(); // [1, 2, 3]
collect([['t' => 'a'], ['t' => 'a'], ['t' => 'b']])->unique('t')->count(); // 2Items that appear more than once.
collect([1, 2, 2, 3, 3])->duplicates()->count(); // 4Seedable.
collect([1, 2, 3])->shuffle()->count(); // 3Implementation: Concerns/Collection/Aggregates.php.
collect([1, 2, 3])->sum(); // 6
collect([['n' => 1], ['n' => 2]])->sum('n'); // 3collect([1, 2, 3, 4])->avg(); // 2.5collect([3, 1, 2])->min(); // 1
collect([3, 1, 2])->max(); // 3collect([1, 2, 3])->median(); // 2
collect([1, 2, 3, 4])->median(); // 2.5collect(['a', 'b', 'c'])->implode(','); // 'a,b,c'
collect([['n' => 'a'], ['n' => 'b']])->implode(',', 'n'); // 'a,b'collect(['a', 'b', 'c'])->join(', ', ' and '); // 'a, b and c'Implementation: Concerns/Collection/Conditional.php.
Forward the Collection through a callback.
collect([1, 2, 3])->pipe(fn ($c) => $c->sum()); // 6Side effect; returns the Collection.
collect([1, 2])->tap(fn ($c) => logger("count={$c->count()}"))->count(); // 2Run $cb only when $cond is truthy.
collect([1, 2])->when(true, fn ($c) => $c->push(3))->all(); // [1, 2, 3]collect([1])->unless(false, fn ($c) => $c->push(2))->all(); // [1, 2]collect([])->whenEmpty(fn ($c) => $c->push(1))->all(); // [1]
collect([1])->whenNotEmpty(fn ($c) => $c->push(2))->all(); // [1, 2]