array_keys

Returns all the keys of an array, or only the keys whose value matches a given search value.

Signature
array_keys(array $array): array
array_keys(array $array, mixed $filter_value, bool $strict = false): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array whose keys are returned.
$filter_valueoptionalmixedIf given, only the keys whose value matches this are returned.
$strictoptionalbool= falseUse === instead of == when matching $filter_value. Only meaningful when $filter_value is supplied.

Return value

arrayA list of the array's keys, renumbered from 0. When $filter_value is given, only the keys holding that value are returned.

Errors

Passing a non-array as $array throws a TypeError.

array_keys() has two jobs. With one argument it returns every key as a fresh zero-indexed list, which is how you iterate over field names, count distinct keys, or feed the keys into array_map(). With a search value it becomes "find every key holding this value" — the plural form of array_search(), which stops at the first hit.

Turn on strict mode

Without the third argument, matching uses loose == comparison. In PHP 8 that no longer makes 0 equal to "apple", but 1, "1", true and 1.0 are all still interchangeable, and so are null, false, 0 and "" in various combinations. Pass true as the third argument whenever the values are not obviously of one type.

What the keys look like coming back

Keys come back as PHP stored them, not as you wrote them. Numeric strings became integers on insertion, true became 1, and null became the empty string. So array_keys(['1' => 'a']) gives [0 => 1] with an integer, which matters if you compare the results with ===.

Not found is empty

A search that matches nothing returns [], never false. Test with count() or an emptiness check, not === false.

Example

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

When to use it

  • List the column names present in a fetched database row.
  • Find every index in a status array that holds 'failed' so you can retry all of them.
  • Rebuild a lookup with array_combine(array_keys($a), $newValues) after transforming values.

Frequently asked questions

What is the difference between array_keys and array_search?

array_search returns the first key holding a value, or false if there is none; array_keys with a search value returns every matching key as an array, and an empty array when nothing matches.

Does array_keys preserve the original keys?

No. The returned array is a fresh zero-indexed list whose values are the original keys. Use array_flip() if you want those keys back as keys.

Why did my string key come back as an integer?

PHP normalises canonical integer strings to integers when they are used as array keys, so "12" is stored as 12 long before array_keys sees it. Non-canonical forms like "012" or " 12" stay strings.

More examples

array_keys versus array_search

array_search stops at job1, while array_keys collects job1 and job3. A search that finds nothing gives an empty array, so never test the result with === false.

Example · php
<?php
$status = ['job1' => 'failed', 'job2' => 'ok', 'job3' => 'failed'];

var_dump(array_search('failed', $status));   // first match only
print_r(array_keys($status, 'failed'));      // every match
var_dump(array_keys($status, 'missing'));    // empty array, not false

Keys come back in their stored form

'12' was normalised to the integer 12 and true to the integer 1, but '012' stays a string because it is not a canonical integer. The strict comparison confirms the exact stored types.

Example · php
<?php
$a = ['12' => 'x', '012' => 'y', true => 'z'];

var_dump(array_keys($a));
var_dump(array_keys($a) === [12, '012', 1]);
echo count($a), " keys stored\n";

Discussion

  • Be the first to comment on this lesson.