w3resource

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:

php function reference: array_keys() function

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

PHP Function Reference

Previous:array_key_exists
Next: array_map



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);