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 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…

PHP Prepared Statements in MySQL: Preventing SQL Injection

MySQL Prepared Statements in PHP are an important tool for ensuring security and optimization. They safeguard against SQL injection attacks,…

Learn How to Insert Documents in MongoDB Using PHP

Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…

Get Last Insert ID in PHP with MySQL PDO and MySQLi

Keeping track of the last record inserted is often important. Whether you are managing user registrations, processing orders, or handling…

IF-Else: Executes the IF-Else Statement in PHP

Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

PHP substr Function: How to Extract and Manipulate Strings

The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

How to Insert Data into MySQL with PHP

pplications. Such As as From registering new users to collecting form submissions and storing product details. Things like adding a…

Traits in PHP: What They Are & Examples

PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…

PHP strict_types: How the Strict Mode Works

If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…

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.