PHP array_keys: How to Extract Keys in Arrays with Examples

php array_keys

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a value.

What is PHP array_keys?

The PHP array_keys function finds all keys in an array. It takes an array and returns its keys in a new array. You can also pass an optional value and strict mode to filter the keys.

Here is the syntax:

array_keys($array, $value = null, $strict = false)
  • The first parameter is the array that holds data.
  • The second parameter is an optional value to match.
  • The third parameter is strict mode. It matches keys only with the same type and value.

Here is an example:

$data = ["a" => 10, "b" => 20, "c" => 10];
$keys = array_keys($data);
print_r($keys);

This code takes an array with three elements and returns all keys. It prints ["a","b","c"].

You can pass a value as the second parameter. PHP will return only keys that hold that value.

$data = ["a" => 10, "b" => 20, "c" => 10];
$keys = array_keys($data, 10);
print_r($keys);

This code returns only keys with value 10. It prints ["a","c"].

Examples of the array_keys Function in PHP

Basic Use:

$items = ["pen" => 5, "book" => 2, "bag" => 1];
$keys = array_keys($items);
print_r($keys);

This code takes an array with three items. It returns all keys from that array. The output is ["pen","book","bag"]. This shows the base use of array_keys.

Filter Keys by Value:

$grades = ["Ali" => 90, "Sara" => 80, "Omar" => 90];
$keys = array_keys($grades, 90);
print_r($keys);

This code takes a list of names and scores. It returns only the names with score 90. The output is ["Ali","Omar"]. This shows how to filter keys by value.

Use Strict Mode:

$data = ["x" => "5", "y" => 5, "z" => 5];
$keys = array_keys($data, 5, true);
print_r($keys);

This code holds numbers and strings. It returns only keys with value and type equal to 5. The output is ["y","z"]. This shows strict comparison with array_keys.

Nested Array Keys:

$data = ["user1" => ["age" => 20], "user2" => ["age" => 25]];
$keys = array_keys($data);
print_r($keys);

This code has a nested array of users. It returns the main keys from that array. The output is ["user1","user2"]. This shows that array_keys does not go deep but only reads the first level.

Wrapping Up

You learned what PHP array_keys does and how it collects keys from arrays. You also learned how to filter keys by value, how to use strict mode, and how it differs from array_values.

Here is a quick recap:

  • array_keys collects keys from arrays.
  • You can filter keys by value or by strict match.
  • array_values collects values from arrays.

FAQs

What is PHP array_keys function used for?

  • PHP array_keys() is used to return all keys from an array.
  • You can also filter keys by a specific value.

<?php
$fruits = array("a" => "Apple", "b" => "Banana", "c" => "Apple");
print_r(array_keys($fruits));
print_r(array_keys($fruits, "Apple"));
?>

What is the syntax of PHP array_keys function?


array_keys(array $array, mixed $search_value = null, bool $strict = false): array
  • $array: The input array.
  • $search_value: Optional, filter keys by value.
  • $strict: Optional, checks type when set to true.

How to get keys by specific value using array_keys?


<?php
$students = array("id1" => "John", "id2" => "Alice", "id3" => "John");
$result = array_keys($students, "John");
print_r($result);
?>
  • This will return all keys where value is John.

What is the difference between array_keys and array_values?

  • array_keys: Returns only the keys from the array.
  • array_values: Returns only the values from the array.

<?php
$data = array("x" => 100, "y" => 200);
print_r(array_keys($data));   // Outputs: Array ( [0] => x [1] => y )
print_r(array_values($data)); // Outputs: Array ( [0] => 100 [1] => 200 )
?>

Similar Reads

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

PHP Logical Operators | Understanding the 6 Logical Operators

The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…

PHP str_replace Function: How it Works with Examples

If you find a word that does not fit and want to fix it. You can use the PHP str_replace…

File Handling in PHP

File Handling in PHP is an important tool especially when you need to manage files such as reading, writing, or…

PHP NOT ( ! ) Operator: A Comprehensive Guide

The NOT operator in PHP (!) provides the reverse of whatever truth value is given by a certain expression. It’s…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP Escape Characters: How to Escape Special Characters

Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…

PHP array_count_values: How it Works with Examples

Developers use the array_count_values function in PHP to find how many times each value appears in an array. What is…

PHP Data Types: Understanding the 10 Primitive Types

The whole world of PHP programming revolves around data, be it numbers, text, or more complicated structures. Each of these…

PHP Type Casting: How to Convert Variables in PHP

Type casting in PHP refers to the process of converting a variable from one data type to another. This is…

Previous Article

Forms in React: How to Create and Manage User Inputs

Next Article

JavaScript with Function Guide: Syntax with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.