Definition and Usage
- PHP Version
- 4+
array_keys()
function extracts only the keys from a given array and returns them as a new array.
This function is especially useful when working with associative arrays, such as handling lists of keys for settings, options, or other important items in web development.
Features
- You can retrieve all keys from the array.
- You can apply a filter option to extract only the keys that match a specific value.
- For indexed arrays (non-associative arrays), the keys are numeric indexes (integers).
- If no keys are found, an empty array is returned.
- The original array remains unchanged.
Basic Example
/** Associative Array **/
$assoc = ['name' => 'Alice', 'age' => 25];
// Get all keys
$keys = array_keys($assoc);
var_dump($keys); // Output: array(2) { [0]=> string(4) "name" [1]=> string(3) "age" }
// Apply filter: get only keys that match a specific value
$keys_filtered = array_keys($assoc, 25);
var_dump($keys_filtered); // Output: array(1) { [0]=> string(3) "age" }
/** Indexed Array **/
$indexed = ['apple', 'banana', 'cherry'];
$keys_indexed = array_keys($indexed);
var_dump($keys_indexed); // Output: array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) }
// Note: array_keys() does not modify the original array.
Syntax
array_keys(array $array): array
array_keys(array $array, mixed $filter_value, bool $strict = false): array
Parameters
$array |
The array from which to extract keys |
---|---|
$filter_value |
Optional. The value used to filter keys. Only keys that match this value are returned.
If this parameter is omitted, all keys are returned. |
$strict |
Optional. Determines whether the comparison with $filter_value should be strict (true ) or not (false ), considering both value and type.
|
Return Values
Returns an array containing all keys from the given array.
If a filter value is specified, returns an array containing only the keys that match the specified value. Returns an empty array if no matching keys are found.
Practical Example
The following example demonstrates a practical use of the array_keys()
function.
Retrieving Website Setting Keys
This example shows how to extract important keys from a website's settings array and display them as a list.
// Associative array containing important website settings
$site_settings = [
'site_name' => 'My Awesome Website',
'admin_email' => 'admin@example.com',
'debug_mode' => true,
'posts_per_page' => 10
];
// Use array_keys() to get the list of setting keys
$setting_keys = array_keys($site_settings);
echo "<h2>Website Setting Keys</h2>";
echo "<ul>";
foreach ($setting_keys as $key) {
echo "<li>" . $key . "</li>";
}
echo "</ul>";
The resulting HTML output would look like this:
<h2>Website Setting Keys</h2>
<ul>
<li>site_name</li>
<li>admin_email</li>
<li>debug_mode</li>
<li>posts_per_page</li>
</ul>
In this example, the array_keys()
function extracts the keys from the $site_settings
array—such as site_name
and admin_email
—and stores them in a new array called $setting_keys
. This list of keys is especially useful for dynamically handling configuration items or generating a UI that displays configurable settings to users.
Although most tasks can be handled using a foreach()
loop without array_keys()
, this function is especially useful for extracting a list of keys. Whether to use it depends on the purpose of your task and the convenience it provides.
References
See also
- PHP foreach Loop
- PHP in_array() Function – Check if a Value Exists in an Array
- PHP array_map() Function – Concepts and Practical Examples
- PHP array_filter() Function – Filter Elements with a Callback
- PHP array_key_exists() Function – Check if a Key Exists in an Array
- PHP array_search() Function – Searching for a Value in an Array
- PHP array_reduce() Function – Reducing an Array to a Single Value