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
You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…
At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…
Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…
Connecting PHP to MongoDB allows you to integrate database functionalities into your web applications. MongoDB is a NoSQL database that…
The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…
PHP math may sound like a pretty simple thing, but I assure you it's a set of tools that will…
PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…
In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…
PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…