Backend Development 5 min read

Using PHP array_keys() Function: Syntax, Parameters, and Practical Examples

This article explains the PHP array_keys() function, detailing its syntax, optional parameters, and demonstrating its use with three code examples that cover indexed, associative, and multidimensional arrays, helping developers retrieve array keys efficiently.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP array_keys() Function: Syntax, Parameters, and Practical Examples

PHP provides many powerful array handling functions, and array_keys() is a particularly useful one that returns a new array containing all the keys of a given array.

The syntax of array_keys() is as follows:

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

Parameters:

$array : The input array from which to retrieve the keys.

$search_value (optional): If provided, only keys whose corresponding values are equal to $search_value are returned.

$strict (optional): When set to true , strict comparison (both type and value) is used.

Below are several examples illustrating how to use array_keys() :

Example 1 – Indexed array:

<?php
// Create an indexed array
$fruits = array("apple", "banana", "orange", "apple", "grape");
// Get all keys
$keys = array_keys($fruits);
// Print the result
print_r($keys);
?>

Output:

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

The example creates an array with duplicate values, uses array_keys() to retrieve all index keys, stores them in $keys , and prints the resulting key list.

Example 2 – Associative array:

<?php
// Create an associative array
$student_scores = array("Mike" => 85, "John" => 92, "Sarah" => 78);
// Get all keys
$keys = array_keys($student_scores);
// Print the result
print_r($keys);
?>

Output:

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

This example shows how array_keys() extracts the names (keys) from an associative array of student scores.

Example 3 – Multidimensional array:

<?php
// Create a multidimensional array of students
$students = array(
    array("name" => "Mike", "age" => 20),
    array("name" => "John", "age" => 22),
    array("name" => "Sarah", "age" => 19)
);
// Get keys of the first student's sub‑array
$names = array_keys($students[0]);
// Print the result
print_r($names);
?>

Output:

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

In this case, array_keys() retrieves the field names (keys) of the first element in a multidimensional array.

Through these examples, we can see that array_keys() is a powerful and flexible function for obtaining all keys from indexed, associative, or even multidimensional arrays, making it highly useful in real‑world PHP development.

Summary

In PHP, the array_keys() function returns a new array containing all keys of the given array, regardless of whether the array is indexed, associative, or multidimensional. Its concise syntax and flexibility make it an essential tool for developers.

backend-developmentPHPcoding tutorialarray-functionsarray_keys
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.