The Ultimate Guide to PHP's array_keys Function
PHP is a widely-used open source programming language, and its array_keys function is an essential tool for PHP developers. In this article, we will explore the
array_keys() returns the keys (the indices) of an array as a new, numerically re-indexed array. It is one of the most common ways to inspect an array's structure: you reach for it whenever you care about which slots an array has rather than the values stored in them.
This chapter explains the syntax, the optional value-search mode, the difference between loose and strict comparison, and the everyday patterns where array_keys() is the right tool.
What array_keys() does
Every PHP array is an ordered map of key → value pairs. The key can be an integer (as in a plain indexed array) or a string (as in an associative array). array_keys() throws away the values and hands you back just the keys, in their original order, re-numbered from 0:
- Pass it one array and you get every key.
- Pass it a search value and you get only the keys whose value matches that search value.
The result is always a fresh array, so the original is never modified. If you instead want the values, use array_values(); if you only need to know whether one specific key is present, array_key_exists() is cheaper.
Syntax
array_keys(array $array, mixed $search_value = ?, bool $strict = false): array| Parameter | Required | Description |
|---|---|---|
$array | Yes | The array to read the keys from. |
$search_value | No | If given, only keys whose value equals this are returned. |
$strict | No | When true, uses strict (===) comparison for the search, so types must match too. Defaults to false. |
The function always returns an array; with no matches it returns an empty array, never false.
Getting all the keys of an array
The most common use is to pull every key out of an associative array:
Output:
Array
(
[0] => a
[1] => b
[2] => c
)The returned $keys array holds the three string keys "a", "b", and "c", re-indexed from 0. The values (apple, banana, cherry) are gone — only the keys remain.
Finding the keys for a value
Pass a second argument and array_keys() becomes a search: it returns the key of every element equal to that value. This is the main difference from array_search(), which stops at the first match.
Output:
Array
(
[0] => 0
[1] => 3
)"apple" appears at positions 0 and 3, so both keys come back. If the value is not found at all, you get an empty array.
Loose vs. strict comparison
By default the value search uses loose comparison (==), so 1, "1", and 1.0 are all treated as equal. Set the third argument to true to require an exact type-and-value match (===):
<?php
$values = array("1", 1, "one", 1, "1");
// Loose: matches both strings and integers
print_r(array_keys($values, 1));
// Strict: matches only the integer 1
print_r(array_keys($values, 1, true));
?>Output:
Array
(
[0] => 0
[1] => 1
[2] => 3
[3] => 4
)
Array
(
[0] => 1
[1] => 3
)The loose search matches "1", 1, 1, and "1" (keys 0, 1, 3, 4), while the strict search only matches the two integer 1s (keys 1 and 3). Reach for $strict = true whenever your array mixes strings and numbers and the distinction matters.
Keys of a multidimensional array
array_keys() only looks at the top level — it returns the keys of the outer array and never descends into nested arrays. With a multidimensional array you get the outer keys back:
<?php
$matrix = array(
"row1" => array("a" => 1, "b" => 2),
"row2" => array("c" => 3),
);
print_r(array_keys($matrix));
?>Output:
Array
(
[0] => row1
[1] => row2
)To collect inner keys too, loop over the result and call array_keys() on each sub-array.
Looping over keys
A frequent pattern is iterating a structure by its keys. Because array_keys() gives a clean list, you can drive a foreach loop directly:
<?php
$prices = array("pen" => 1.20, "book" => 5.00, "bag" => 12.50);
foreach (array_keys($prices) as $item) {
echo $item . PHP_EOL;
}
?>Output:
pen
book
bagIn day-to-day code you would often loop with foreach ($prices as $item => $price) instead — but array_keys() is handy when you need the key list as a value (to pass it somewhere, count it, or compare two arrays' keys).
Common use cases and gotchas
- Counting keys:
count(array_keys($arr))is the same ascount($arr)— prefercount($arr)directly. - Checking a key exists: don't do
in_array($k, array_keys($arr))— usearray_key_exists()orisset(), which are faster and clearer. (To test for a value, usein_array().) - Result is always re-indexed: the returned array's own keys are
0, 1, 2, …regardless of the original keys. - No matches returns
[]: the value-search form never returnsfalse, so test it withempty()orcount(), not=== false.
Conclusion
array_keys() extracts the keys of an array, optionally filtered by value with either loose or strict comparison. Use it to list keys, find every position of a value, or feed a key list into a loop. For the matching values use array_values(), to test a single key use array_key_exists(), and to find the first match of a value use array_search().