In this tutorial, you shall learn about PHP array_keys() function which can get the keys in an array, with syntax and examples.

PHP array_keys() Function

The PHP array_keys() function returns keys of a given array. You can also specify a value, so that array_keys() returns only those keys whose value matched this value.

array_keys() function returns keys as an indexed array.

Syntax of array_keys()

The syntax of array_keys() function is

</>
Copy
array_keys( array, value, strict)

where

ParameterDescription
array[mandatory] The array which we have to work on.
value[optional] The keys in the array, which has this value only, will be returned.
strict[optional] Used with the value parameter. If strict is TRUE, then the type of value is also matched. If strict is FALSE, then the type of value is not considered for comparison of values.

Function Return Value

array_keys() returns an array containing the keys of given array.

Examples

1. Get keys in the given array

In this example, we will take an array with key-value pairs. We will call array_keys() function with the array provided as argument, to get all the keys in the array, and then print the keys array.

PHP Program

</>
Copy
<?php
$array1 = array("a"=>21, "b"=>54, "m"=>35, "k"=>66);
$keys = array_keys($array1);
print_r($keys);
?>

Output