Skip to main content

How to Use PHP array_keys() – Examples & Best Practices

1. What is array_keys()?

  • Extracts keys from an array.

  • Returns a new indexed array containing the keys.

2. Syntax

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

3. Parameters

  • $array → Input array.

  • $search_value (optional) → Only return keys matching this value.

  • $strict (optional) → Use strict comparison (===).

4. Return Value

  • An array of keys.


5. Practical Examples

Example 1: Basic Key Extraction

php
$user = ['name' => 'Alice', 'age' => 25, 'role' => 'Admin'];  
$keys = array_keys($user);  
print_r($keys);  

Output:

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

Example 2: Filter Keys by Value

php
$colors = ['red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF'];  
$keys = array_keys($colors, '#00FF00');  
print_r($keys);  

Output:

Array ( [0] => green )  

Example 3: Strict Comparison

php
$data = ['a' => '1', 'b' => 1, 'c' => true];  
$keys = array_keys($data, 1, true); // Strict check (type-sensitive)  
print_r($keys);  

Output:

Array ( [0] => b ) // '1' (string) and true are excluded  

6. Use Cases

  • Dynamic Forms: Get field names from config arrays.

  • Data Validation: Check if required keys exist.

  • API Responses: Extract headers or parameters.


7. Performance Note

  • Works in O(n) time (scales linearly with array size).

  • Avoid nested loops with large arrays.


8. Edge Cases

  • Returns empty array if input isn’t an array.

  • null values are treated like any other value.


9. Alternatives

  • array_key_exists() → Check if a key exists.

  • array_values() → Extract values instead of keys.

Comments

Popular posts from this blog

PHP array_column() Function

  <?php // An array that represents a possible record set returned from a database $a =  array (    array (      'id'  =>  5698 ,      'first_name'  =>  'Mehran' ,      'last_name'  =>  'Yaqoob' ,   ),    array (      'id'  =>  4767 ,      'first_name'  =>  'Muneeb' ,      'last_name'  =>  'Ahmad' ,   ),    array (      'id'  =>  3809 ,      'first_name'  =>  'Uzair' ,      'last_name'  =>  'Rajput' ,   ) ); $last_names = array_column($a,  'last_name' ); print_r($last_names); ?> Output: Array (   [0] => Yaqoob   [1] => Ahmad   [2] => Rajput ) Syntex: array_column( array ,  column_key ,  index_key ...

Mastering PHP's array_merge(): When to Use It (And When Not To)

Pros, Cons, and Best Practices PHP's  array_merge()  function is one of the most commonly used array functions in web development. Whether you're building a simple website or a complex web application, understanding how to effectively merge arrays can save you time and prevent headaches. In this comprehensive guide, we'll explore everything you need to know about  array_merge() , including its advantages, limitations, practical use cases, and alternatives. What is  array_merge()  in PHP? array_merge()  is a built-in PHP function that combines two or more arrays into a single array. The function takes multiple array arguments and returns a new array containing all the elements from the input arrays. Basic Syntax php array_merge ( array ... $arrays ) : array Simple Example php $array1 = [ 'a' , 'b' , 'c' ] ; $array2 = [ 'd' , 'e' , 'f' ] ; $result = array_merge ( $array1 , $array2 ) ; print_r ( $result ) ; /* O...