Worldscope

PHP array_keys() Function

Palavras-chave:

Publicado em: 05/08/2025

Understanding the PHP array_keys() Function

The array_keys() function in PHP is a powerful tool for extracting all or a subset of keys from an array. This article provides a comprehensive guide to using array_keys(), covering its syntax, usage, and complexity, along with alternative approaches.

Fundamental Concepts / Prerequisites

Before diving into array_keys(), you should have a basic understanding of PHP arrays. Arrays are ordered maps that hold key-value pairs. You should also be familiar with the concept of keys and values within an array. The key is the identifier for a particular element in the array, while the value is the data associated with that key.

Core Implementation/Solution

The array_keys() function has the following syntax:


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

Here's an example of how to use array_keys():


<?php

// Example 1: Getting all keys from an associative array
$my_array = array("name" => "John", "age" => 30, "city" => "New York");
$keys = array_keys($my_array);
print_r($keys); // Output: Array ( [0] => name [1] => age [2] => city )

// Example 2: Getting keys based on a specific value
$my_array = array("a" => "red", "b" => "green", "c" => "red");
$keys = array_keys($my_array, "red");
print_r($keys); // Output: Array ( [0] => a [1] => c )

// Example 3: Using strict comparison
$my_array = array(10 => "red", "10" => "green"); // Note 10 (int) and "10" (string)
$keys = array_keys($my_array, "red", true); // Strict comparison
print_r($keys); // Output: Array ( [0] => 10 )

$keys = array_keys($my_array, "red", false); // Non-strict comparison
print_r($keys); // Output: Array ( [0] => 10 )
?>

Code Explanation

Example 1: This example retrieves all the keys from the $my_array associative array. The array_keys() function returns a new array containing only the keys: "name", "age", and "city".

Example 2: This example retrieves keys that correspond to the value "red". Only keys "a" and "c" have the value "red", so those are returned.

Example 3: This example demonstrates the use of the optional $strict parameter. When set to true, the comparison is performed strictly (i.e., the types must match). In this case, only the key 10 (integer) matches the value "red" with strict comparison. If strict is false (or omitted), "10" and `10` are treated as the same.

Complexity Analysis

The array_keys() function has a time complexity of O(n), where n is the number of elements in the input array. This is because the function needs to iterate through each element of the array to extract its key. When a $search_value is provided, the function needs to compare each value with the search value. The space complexity is also O(n) in the worst case. This occurs when all keys are returned. If the search value argument is provided and only a few keys match, then the space complexity is proportional to the number of keys that match the search value, but cannot exceed O(n).

Alternative Approaches

While array_keys() is the most straightforward way to get array keys, you could also use a loop (foreach) to iterate through the array and manually build an array of keys. However, this is less efficient and more verbose than using array_keys().


<?php

$my_array = array("name" => "John", "age" => 30, "city" => "New York");
$keys = [];
foreach ($my_array as $key => $value) {
    $keys[] = $key;
}
print_r($keys); // Output: Array ( [0] => name [1] => age [2] => city )

?>

This alternative using `foreach` is functionally equivalent for simply extracting keys but is less efficient because of the overhead of managing the loop and manually appending elements to the `$keys` array. Using `array_keys()` is the recommended method for clarity and performance.

Conclusion

The array_keys() function in PHP provides a simple and efficient way to extract all or a subset of keys from an array. Understanding its syntax, usage, and complexity can help you write cleaner and more efficient PHP code. When in doubt, use the built-in array_keys() function as it is generally the most performant and readable approach for this task.