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

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

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

PHP provides a powerful set of array handling functions, and array_keys() is one of the most useful. It returns a new array containing all the keys from a given array.

The syntax of array_keys() is:

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

Parameters: $array: The input array whose keys are to be retrieved. $search_value (optional): If provided, only keys whose corresponding values are equal to this value are returned. $strict (optional): When true, the comparison uses strict type checking.

Below are three examples demonstrating different use cases.

Example 1 – Indexed array:

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

Output:

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

Example 2 – Associative array:

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

Output:

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

Example 3 – Multidimensional array (first element keys):

<?php
$students = array(
    array("name" => "Mike", "age" => 20),
    array("name" => "John", "age" => 22),
    array("name" => "Sarah", "age" => 19)
);
$names = array_keys($students[0]);
print_r($names);
?>

Output:

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

These examples show that array_keys() works with indexed, associative, and even multidimensional arrays, making it a versatile tool for retrieving keys in PHP development.

In summary, the array_keys() function is concise, flexible, and highly practical for real‑world backend programming.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode ExamplesArraysarray_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

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.