Collection::keys

Returns a new collection containing all of the original collection's keys as values.

Signature
public function keys(): static

Return value

Illuminate\Support\CollectionA new, numerically indexed collection whose values are the original collection's keys.

keys is a thin wrapper over array_keys: it returns a fresh, zero-indexed collection whose values are the keys of the original. The values of the original are thrown away.

When to reach for it

Mostly after something has already keyed the data — keyBy, groupBy, mapWithKeys or pluck with a key argument. Once you have a lookup table, keys gives you the set of identifiers you can then diff, intersect, sort or feed straight into a whereIn query. It is also the quickest way to inspect the shape of an associative row without dumping the whole payload.

What trips people up

Two things. First, the result is re-indexed from zero, so the position of a key in the output has nothing to do with the original structure — chaining sort() without values() afterwards leaves you with shuffled numeric indexes. Second, PHP array key rules still apply: numeric-looking string keys such as '10' come back as integers, which matters when you compare key sets with a strict in_array or a typed check.

Example

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

When to use it

  • Diff the identifiers of two keyed collections to find records that exist on one side only.
  • Inspect which columns an imported row actually contains before validating it.
  • Pull the id list out of a keyBy result to pass into a whereIn lookup.

Frequently asked questions

How do I get the keys of a collection as a plain PHP array?

Call all() on the result: $collection->keys()->all(). The method itself always returns a collection so the call can keep chaining.

Does it preserve the original keys?

No — the returned collection is a zero-indexed list whose values are the old keys. Use flip() if you want the keys to stay keys.

Why did my string key '10' come back as the integer 10?

PHP converts numeric strings to integers when they are used as array keys, so the conversion happened when the array was built, not by this method. Compare keys loosely or cast them if the type matters.

More examples

Diffing two keyed datasets

Reducing both sides to their key sets makes a sync check trivial: 'a1' exists only locally and 'd4' only remotely. Because diff preserves keys, implode is used rather than all() so the output reads as a plain list.

Example · php
<?php

$local = collect([
    'a1' => 'Desk',
    'b2' => 'Chair',
    'c3' => 'Lamp',
])->keys();

$remote = collect([
    'b2' => 'Chair',
    'c3' => 'Lamp',
    'd4' => 'Rug',
])->keys();

echo 'missing remotely: ' . $local->diff($remote)->implode(', ') . PHP_EOL;
echo 'new remotely: ' . $remote->diff($local)->implode(', ') . PHP_EOL;

Checking the shape of imported rows

keys() turns the row into the set of columns it actually carries, so a diff against the required list reports 'price' as missing. The extra 'colour' column is simply ignored by this check.

Example · php
<?php

$required = ['sku', 'name', 'price'];

$row = collect([
    'sku' => 'A1',
    'name' => 'Desk',
    'colour' => 'oak',
]);

$present = $row->keys();

print_r($present->all());
echo 'missing: ' . collect($required)->diff($present)->implode(', ') . PHP_EOL;

Discussion

  • Be the first to comment on this lesson.