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

This article explains PHP's array_keys() function, detailing its syntax, optional parameters, and demonstrating its use with indexed, associative, and multidimensional arrays through clear code examples and output illustrations.

php Courses
php Courses
php Courses
Using PHP's 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 for retrieving all key names from a given array.

The function signature is:

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

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

Example 1 – Indexed array:

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

Output:

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

Example 2 – Associative array:

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

Output:

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

Example 3 – Multidimensional array:

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

Output:

Array<br/>(<br/>    [0] => name<br/>    [1] => age<br/>)

These examples demonstrate that array_keys() works seamlessly with indexed, associative, and even multidimensional arrays, making it a versatile tool for extracting key information in PHP development.

Summary

In PHP, the array_keys() function returns a new array containing all the keys from the input array, regardless of whether the array is indexed, associative, or multidimensional. Its concise syntax and flexible parameters make it extremely useful in everyday 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.

PHPTutorialarray 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

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.