PHP array keys

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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

Pictorial Presentation :

php-array-array_keys()

Example - 1 :

  1. $array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);  

  2. print_r(array_keys($array1));  

  3. ?>   

Output :

Array ( [0] => Orange [1] => Apple [2] => Banana [3] => Cherry )

Practice here online :

Example - 2 :

  1. $array1=array("Orange","Apple","Banana","Apple");  

  2. print_r(array_keys($array1,"Apple"));  

  3. ?>  

Output :

Array ( [0] => 1 [1] => 3 )