The array_keys() function returns an array containing all keys found in the searched array. Its form is as follows:
array array_keys(array array[,mixed search_value])
If the optional parameter search_value is included, only keys matching the value will be returned. The following example will output all arrays found in the $fruit array:
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $keys = array_keys($fruits); print_r($keys); //Array ( [0] => apple [1] => banana [2] => watermelon )
Returns a new array containing all the key names in the array:
<?php $a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a)); ?>
Definition and Usage
array_keys () function returns a new array containing all the keys in the array.
Syntax
array_keys(array,value,strict)
array Required. Specifies an array.
value Optional. You can specify a key value, and then only the key name corresponding to that key value will be returned.
strict Optional. Used with the value parameter. Possible values:
true - Returns the key name with the specified key value. Depending on the type, the number 5 is not the same as the string "5".
false - the default value. Independent of type, the number 5 is the same as the string "5".
Return value:
Returns a new array containing all the key names in the array.
Parameters:
<?php $a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a,"Highlander")); ?>
Use strict parameter (false):
<?php $a=array(10,20,30,"10"); print_r(array_keys($a,"10",false)); ?>
Use strict parameter (true):
<?php$a=array(10,20,30,"10");print_r(array_keys($a,"10",true));?>