PHP array_keys() Function

The array_keys() function returns an array containing the keys.

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.

ParameterDescription
arrayRequired. Specifies an array
valueOptional. You can specify a value, then only the keys with this value are returned
strictOptional. Used with the value parameter. Possible values:true – Returns the keys with the specified value, depending on type: the number 5 is not the same as the string “5”.false – Default value. Not depending on type, the number 5 is the same as the string “5”.

Syntax

 array_keys(array,value,strict) 

Example

$array=array("a"=>"Prince","b"=>"Samuel","c"=>"Cooper");
print_r(array_keys($array,"Cooper"));

Output

Array ( [0] => c ) 

More Example

$array=array(4,5,6,"5");
print_r(array_keys($array,"5",false));

Output

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

Leave a Reply

Your email address will not be published. Required fields are marked *