In this tutorial, you shall learn how to get keys of an array in PHP using array_keys() function, with example programs.

PHP – Get keys of an array

To get keys of an associative array in PHP, you can use array_keys() function.

Call array_keys() function, and pass the array as argument. The function returns the keys as an indexed array.

The syntax to get the keys of an array $arr is

array_keys($arr)

Example

In the following program, we take an associative array $arr, and get the keys of this array using array_keys() function.

PHP Program

<?php
$arr = array(
    'foo' => 10,
    'bar' => 20,
    'das' => 30,
    'qux' => 40,
    'par' => 50,
    'saz' => 60,
);

$keys = array_keys($arr);

print_r($keys);
?>

Output

PHP - Get keys of an array
ADVERTISEMENT

Conclusion

In this PHP Tutorial, we learned how to get the keys of an array, using array_keys() function.