PHP 8 reference · Array Functions
PHP array_keys() Function
Return all the keys of an array, or only the keys whose value matches a search value.
PHP 8 Since 4.0
Syntax
array_keys(array $array, mixed $filter_value, bool $strict = false): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
filter_value optional |
Only return keys for this value. |
strict optional |
true compares with ===. Default: false |
Return value
A list of keys.
Examples
<?php
$user = ["name" => "Ada", "role" => "admin", "active" => true];
print_r(array_keys($user));
Output
Array
(
[0] => name
[1] => role
[2] => active
)<?php
$votes = ["ann" => "yes", "bob" => "no", "cal" => "yes"];
print_r(array_keys($votes, "yes"));
Output
Array
(
[0] => ann
[1] => cal
)Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
dict.keys()· PythonObject.keys()· JavaScriptarray.keys()· JavaScript
Related
array_values()· Return all the values of an array as a list re-indexed from 0. Often used after array_filter() or unset().array_key_exists()· Check whether an array has a given key, even if its value is null. isset() returns false for null values.array_search()· Search an array for a value and return the first matching key, or false if it isn't there.array_flip()· Swap keys and values, so each value becomes a key. Values must be strings or integers; duplicates keep the last key.- PHP Arrays · tutorial
- PHP Array Functions · tutorial