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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_keys(array $array, mixed $filter_value, bool $strict = false): array

Parameters

ParameterDescription
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

All keysTry it yourself
<?php
$user = ["name" => "Ada", "role" => "admin", "active" => true];
print_r(array_keys($user));

Output

Array
(
    [0] => name
    [1] => role
    [2] => active
)
Keys for a valueTry it yourself
<?php
$votes = ["ann" => "yes", "bob" => "no", "cal" => "yes"];
print_r(array_keys($votes, "yes"));

Output

Array
(
    [0] => ann
    [1] => cal
)

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?