PHP: array_keys() function
PHP: Return all the keys of an array
The array_keys() function is used to get all the keys or a subset of the keys of an array.
Version:
(PHP 4 and above)
Syntax:
array_keys(input_array, search_key_value, strict)
Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
input_array | Specified array. | Required | Array |
search_key_value | Value to be checked. | Optional | Array |
strict | As of PHP 5, this parameter determines if strict comparison (===) should be used during the search. | Optional | Boolean |
Return value:
An array of all the keys of input_arrray.
Value Type: Array
Example - 1:
<?php
$array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
print_r(array_keys($array1));
?>
Output:
Array ( [0] => Orange [1] => Apple [2] => Banana [3] => Cherry )
Pictorial Presentation:

View the example in the browser
Example - 2 :
<?php $array1=array("Orange","Apple","Banana","Apple"); print_r(array_keys($array1,"Apple")); ?>
Output :
Array ( [0] => 1 [1] => 3 )
View the example in the browser
Practice here online :
See also
Previous:array_key_exists
Next: array_map
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises