array_keys
Returns all the keys of an array.
Available since: PHP 4
Syntax
array_keys(array $array): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The source array. |
Return value
arrayA list of the keys.
Examples
Keys of an associative array:
<?php
print_r(array_keys(['name' => 'Anna', 'age' => 25]));Output:
Array
(
[0] => name
[1] => age
)The companion array_values() returns only the values:
<?php
print_r(array_values(['name' => 'Anna', 'age' => 25]));Output:
Array
(
[0] => Anna
[1] => 25
)Notes
A value may be passed as a second argument — then only the keys of elements with that value are returned.
See also
- in_array — Checks whether a value exists in an array.
- array_search — Searches an array for a value and returns its key.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.