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 Push Multiple Values: A Complete Guide | StackCodee

PHP Array Push Multiple Values: A Complete Guide | StackCodee PHP Array Push Multiple Values: A Complete Guide 📅 November 8, 2025 ⏱️ 8 min read 🏷️ PHP, Arrays, Programming Welcome to StackCodee, your go-to resource for practical programming knowledge. In this comprehensive guide, we'll explore the various methods to push multiple values to a PHP array efficiently. Whether you're a beginner or an experienced developer, understanding these techniques will enhance your array manipulation skills in PHP. Understanding PHP Arrays PHP arrays are incredibly versatile data structures that can hold multiple values of different types. They can be indexed numerically or associatively with key-value pairs, and they can even function as lists, stacks, or queues. ...

PHPMyAdmin Localhost:8080 Setup Guide

PHPMyAdmin Localhost:8080 Setup Guide PHPMyAdmin Localhost:8080 Setup Guide Learn how to install, configure, and troubleshoot PHPMyAdmin running on localhost port 8080 with detailed examples and solutions to common problems. ⏱️ 10 min read 🏷️ PHPMyAdmin, MySQL, Localhost, Web Development Installation Steps 1 Install PHPMyAdmin Download and install PHPMyAdmin on your local server environment (XAMPP, WAMP, MAMP, or manual setup). # For Ubuntu/Debian systems sudo apt-get install phpmyadmin # For CentOS/RHEL systems sudo yum install phpmyadmin # Or download direc...

PHP array_merge_recursive() Function — Explained

PHP array_merge_recursive() — Preserve Numeric Keys 📖 PHP array_merge_recursive() Function — Explained The array_merge_recursive() function in PHP is used to merge two or more arrays recursively. It helps you combine values under the same keys without overwriting any data. Unlike array_merge() , which replaces duplicate keys, this function keeps both values by converting them into arrays. 🔑 Keyword Focus: php array_merge_recursive preserve numeric keys If you're trying to preserve numeric keys while merging arrays in PHP, it's important to know that array_merge_recursive() will reindex numeric keys by default. Let’s see what that means. 📌 Syntax array_merge_recursive(array $array1, array $array2, ...): array Parameters: $array1, $array2, ... – Arrays you want to merge Returns: New merged array. String keys: merged into arrays. Numeric keys: re...