PHP arrays can contain numeric indexes, named keys, or a combination of both. Sometimes you need to retrieve those keys without accessing the values themselves.
The array_keys() function lets you extract all keys from an array or find the keys associated with a particular value.
In this guide, you'll learn how to use array_keys(), search for matching values, and understand how strict comparisons affect the results.
What Is array_keys() in PHP?
The array_keys() function returns a new array containing keys from an existing array.
Its syntax is:
array_keys(
array $array,
mixed $filter_value,
bool $strict = false
): array
Only the first parameter is required:
$array— The array whose keys you want to retrieve.$filter_value— An optional value used to return only matching keys.$strict— Whether matching should use strict comparison (===). Defaults tofalse.
When no filter value is supplied, the function returns every key. The original array remains unchanged.
Example 1: Getting All Array Keys
Let's extract the keys from an associative array containing product information.
<?php
$product = [
"id" => 101,
"name" => "Keyboard",
"price" => 49.99
];
$keys = array_keys($product);
print_r($keys);
Output:
Array
(
[0] => id
[1] => name
[2] => price
)
The original keys become values in a new, sequentially indexed array.
The function preserves their types and order. Integer keys remain integers, and string keys remain strings.
Example 2: Finding Keys with a Specific Value
You can provide a second argument to return only keys associated with a particular value.
<?php
$statuses = [
"TASK-101" => "Completed",
"TASK-102" => "Pending",
"TASK-103" => "Completed",
"TASK-104" => "Pending"
];
$pending = array_keys(
$statuses,
"Pending"
);
print_r($pending);
Output:
Array
(
[0] => TASK-102
[1] => TASK-104
)
Both matching keys are returned. This differs from array_search(), which returns only the first matching key.
Understanding Strict Comparison
By default, array_keys() uses loose comparison when searching for a value. This means values with different types may still match.
<?php
$data = [
"integer" => 0,
"string" => "0",
"other" => 10
];
print_r(array_keys($data, 0));
print_r(array_keys($data, 0, true));
Output:
Array
(
[0] => integer
[1] => string
)
Array
(
[0] => integer
)
With strict comparison enabled, integer 0 and string "0" are treated as different values.
Recommendation: Use true as the third argument when matching values must have the same type.
Practical Example: Finding Orders That Need Processing
Imagine you're building an order management system. Orders are stored in an associative array where each key represents an order ID.
You want to collect the IDs of orders that are still waiting for processing.
Create a file named pending-orders.php:
<?php
declare(strict_types=1);
$orders = [
"ORD-201" => "Shipped",
"ORD-202" => "Pending",
"ORD-203" => "Delivered",
"ORD-204" => "Pending",
"ORD-205" => "Cancelled"
];
// Find all pending order IDs.
$pendingIds = array_keys(
$orders,
"Pending",
true
);
// Display the matching orders.
echo "<h2>Pending Orders</h2>";
if ($pendingIds === []) {
echo "<p>No pending orders.</p>";
} else {
echo "<ul>";
foreach ($pendingIds as $orderId) {
echo "<li>" . htmlspecialchars(
(string) $orderId,
ENT_QUOTES,
"UTF-8"
) . "</li>";
}
echo "</ul>";
}
Browser output:
Pending Orders
ORD-202
ORD-204
The function retrieves every order ID associated with the status "Pending".
This technique is useful for tracking tasks, filtering status records, and identifying multiple entries sharing the same value.
Handling Empty Arrays and Missing Values
If the original array is empty, array_keys() returns an empty array.
The same happens when a specified filter value has no matches.
<?php
var_dump(array_keys([]));
$colors = ["Red", "Blue"];
var_dump(array_keys($colors, "Green"));
Output:
array(0) {
}
array(0) {
}
One important detail: explicitly passing null as the second argument searches for keys whose values are null. It does not mean "return all keys."
The function also examines only the supplied array level. It does not automatically search inside nested arrays.
array_keys() vs Related Functions
| Function | Purpose |
|---|---|
array_keys() |
Returns all keys or keys associated with a matching value. |
array_values() |
Returns array values with sequential numeric indexes. |
array_key_exists() |
Checks whether a specific key exists. |
array_search() |
Returns the first key associated with a matching value. |
array_key_first() |
Returns the first key in array order. |
Use array_keys() when you need a collection of keys rather than a single key or a boolean result.
Key Takeaways
array_keys()returns keys from an existing PHP array.- Without a filter value, it returns every key.
- The optional second argument finds keys associated with matching values.
- Use the third argument to enable strict comparison.
- The resulting array uses sequential numeric indexes.
- Empty arrays and searches without matches return an empty array.
- The original array remains unchanged.
The array_keys() function provides a convenient way to inspect array structure and find entries associated with particular values. It is especially useful when working with associative arrays and collections containing repeated values.
For additional details, see the official PHP array_keys() documentation.