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 filter_input: How to Validate Input Securely

User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…

PHP compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

PHP Integers Guide: Types, Limits, and Conversions

You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…

PHP array_key_first Function: How it Works with Examples

The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

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…

PHP foreach Loop: How to Access Keys & Values in Arrays

PHP foreach loop came to help work with array data in a simple way. Arrays and objects grew more common…

PHP rtrim Function: Remove Trailing Characters or Whitespace

You deal with user input, file reads, or formatted strings. PHP needed a way to remove extra characters from the…

PHP mail() Function: A Complete Guide

PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…

Install PHP on Operating System: Ubuntu, Windows, and macOS

In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…

PHP array_diff_assoc: How to Compare Arrays with Keys

Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…

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.