Backend Development 5 min read

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

This article introduces PHP's array_keys() function, explains its syntax and optional parameters, and provides three practical code examples demonstrating its use with indexed, associative, and multidimensional arrays, helping developers retrieve array keys efficiently.

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

In PHP there are many powerful array handling functions, and array_keys() is a very useful one. The function returns a new array containing all the keys of a given array.

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

Parameters:

$array : The array whose keys you want to retrieve.

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

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

Example 1 – retrieving keys from an indexed array:

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

Output:

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

The code creates a simple indexed array, calls array_keys() to obtain its keys, stores them in $keys , and prints the resulting array of indices.

Example 2 – retrieving keys from an associative array:

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

Output:

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

Here the function extracts the names of the students (the keys) from the associative array.

Example 3 – retrieving keys from a multidimensional array (first element):

<?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 result
print_r($names);
?>

Output:

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

This demonstrates that array_keys() works with multidimensional arrays, returning the keys of the specified inner array.

In summary, the array_keys() function is a concise and flexible tool for obtaining all keys from indexed, associative, or multidimensional arrays in PHP, making it highly useful in everyday development.

Backendarraysphp-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.