PHP array_keys() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_keys() returns all the keys of an array, or only the keys whose value matches a filter.

The result is a new numbered list of keys. Pass a second argument to find every key that holds a certain value.

Syntax

Call array_keys() with the values you need. Start with this line.

PHP
$keys = array_keys($array);
CallWhat it does
array_keys($array)Returns every key of the array.
array_keys($array, "open")Returns only keys whose value equals "open".
array_keys($array, 1, true)Returns keys whose value is exactly the integer 1.

When you pass a filter value, array_keys() can return several keys. To find only the first match, use array_search().

Library declarations
PHP
array_keys(array $array, mixed $filter_value = null, bool $strict = false): array

Parameters

ParameterDescription
arrayRequired. The array to read the keys from.
filter_valueOptional. If given, only keys with this value are returned.
strictOptional. If true, values are compared with === (same type). Default is false.

Return Value

Return ValueDescription
arrayA list of keys. It is empty if nothing matches.

PHP Examples

Example 1 — List All Keys

Get the keys of an associative array as a simple list.

PHP
<?php
$data = ["a" => 1, "b" => 2, "c" => 3];

$keys = array_keys($data);

echo implode(", ", $keys), PHP_EOL;

How It Works

  1. Create an associative array. The keys are a, b and c.
  2. Call array_keys(). It collects the keys into a new array.
  3. Join them. implode() puts a comma between each key.
  4. Print the text. The values 1, 2, 3 are not included.

Example 2 — Loose vs Strict Value Search

The strict flag decides whether 1 and "1" count as the same value.

PHP
<?php
$values = [1, "1", 2, 1, "x"];

echo implode(", ", array_keys($values, 1)), PHP_EOL;
echo implode(", ", array_keys($values, 1, true)), PHP_EOL;

How It Works

  1. Mix numbers and strings. The value 1 appears as an integer and as the string "1".
  2. Search loosely. 1 == "1" is true, so keys 0, 1 and 3 match.
  3. Search strictly. Pass true to require the same type.
  4. Compare the results. Key 1 is dropped because "1" is a string.

Example 3 — Find All Open Days

Use a filter value to collect every key that has the same value.

PHP
<?php
$hours = ["Mon" => "open", "Tue" => "closed", "Wed" => "open"];

$days = array_keys($hours, "open");

echo implode(", ", $days), PHP_EOL;
echo count($days), PHP_EOL;

How It Works

  1. Create the schedule. Keys are days and values are the status.
  2. Filter for "open". PHP returns the keys whose value is open.
  3. Join the days. The result is Mon, Wed.
  4. Count them. Two days are open.

Frequently Asked Questions

It returns the keys of an array as a new numbered list. You can also pass a value to get only the keys that hold that value.
array_keys(array $array, mixed $filter_value = null, bool $strict = false): array.
Pass the value as the second argument, like array_keys($array, "open"). Every matching key is returned.
Use array_values(), which returns the values as a new numbered list.
When true, values must match in both value and type, so 1 does not match "1".

Next: array_last()

array_last() returns the last value of an array, or null if the array is empty.

array_last() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful