Current Location: Home> Latest Articles> PHP array_keys() Function Explained: Get All Keys from an Array

PHP array_keys() Function Explained: Get All Keys from an Array

M66 2025-06-30

PHP array_keys() Function Explained: Get All Keys from an Array

In PHP, the array_keys() function is a very useful tool for array manipulation. It returns all the keys from a given array. Whether the array is indexed, associative, or multidimensional, array_keys() helps you efficiently retrieve the keys. This article will explain how to use the array_keys() function with multiple examples.

Basic Syntax of array_keys() Function

The syntax of the array_keys() function is as follows:

array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] ) : array

Parameters Explanation

  • $array: The array whose keys you want to retrieve.
  • $search_value (optional): If provided, the function will only return the keys that correspond to the specified value.
  • $strict (optional): If set to true, it performs a strict comparison (both type and value must match).

Example 1: Get Keys from an Indexed Array

First, let's create an indexed array containing duplicate values and use array_keys() to get all the keys.

<?php
// Create an array
$fruits = array("apple", "banana", "orange", "apple", "grape");

// Get all the keys from the array
$keys = array_keys($fruits);

// Print the result
print_r($keys);
?>

Output:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
)

In this example, we created an array with duplicate elements. The array_keys() function returns all the keys from the array, and we print the result using the print_r() function.

Example 2: Get Keys from an Associative Array

Next, let's create an associative array containing student names and scores, and use array_keys() to retrieve all the keys.

<?php
// Create an associative array
$student_scores = array("Mike" => 85, "John" => 92, "Sarah" => 78);

// Get all the keys from the array
$keys = array_keys($student_scores);

// Print the result
print_r($keys);
?>

Output:

Array
(
    [0] => Mike
    [1] => John
    [2] => Sarah
)

This example demonstrates how to get all the keys (student names) from an associative array.

Example 3: Get Keys from a Multidimensional Array

In this example, we create a multidimensional array containing student information and retrieve the keys for the first student's data.

<?php
// Create a multidimensional array
$students = array(
    array("name" => "Mike", "age" => 20),
    array("name" => "John", "age" => 22),
    array("name" => "Sarah", "age" => 19)
);

// Get all keys from the first student's data
$names = array_keys($students[0]);

// Print the result
print_r($names);
?>

Output:

Array
(
    [0] => name
    [1] => age
)

This example shows how to use array_keys() to get the keys for the first student's information from a multidimensional array.

Conclusion

The array_keys() function is an essential tool for working with arrays in PHP. Whether the array is indexed, associative, or multidimensional, array_keys() makes it easy to retrieve all the keys from an array. After going through the examples in this article, you should have a clear understanding of how to use this function effectively in your PHP projects. We hope this guide helps you in your development work.